blob: 189c2f07b83f4331af92528886864e0f5c5fa1d7 [file] [log] [blame]
Balaji Raof52046b2009-01-09 01:49:01 +01001/* NXP PCF50633 Power Management Unit (PMU) driver
2 *
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
5 * Balaji Rao <balajirrao@openmoko.org>
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/sysfs.h>
Balaji Raof52046b2009-01-09 01:49:01 +010018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/interrupt.h>
21#include <linux/workqueue.h>
22#include <linux/platform_device.h>
23#include <linux/i2c.h>
Mark Brown939941d2011-01-20 21:52:42 +000024#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Mark Brown6e3ad112011-08-08 17:04:40 +090026#include <linux/regmap.h>
27#include <linux/err.h>
Balaji Raof52046b2009-01-09 01:49:01 +010028
29#include <linux/mfd/pcf50633/core.h>
30
Lucas De Marchi25985ed2011-03-30 22:57:33 -030031/* Read a block of up to 32 regs */
Balaji Raof52046b2009-01-09 01:49:01 +010032int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
33 int nr_regs, u8 *data)
34{
35 int ret;
36
Mark Brown6e3ad112011-08-08 17:04:40 +090037 ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
38 if (ret != 0)
39 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +010040
Mark Brown6e3ad112011-08-08 17:04:40 +090041 return nr_regs;
Balaji Raof52046b2009-01-09 01:49:01 +010042}
43EXPORT_SYMBOL_GPL(pcf50633_read_block);
44
Lucas De Marchi25985ed2011-03-30 22:57:33 -030045/* Write a block of up to 32 regs */
Balaji Raof52046b2009-01-09 01:49:01 +010046int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
47 int nr_regs, u8 *data)
48{
Axel Lin60b5c5a42012-02-14 11:30:31 +080049 return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
Balaji Raof52046b2009-01-09 01:49:01 +010050}
51EXPORT_SYMBOL_GPL(pcf50633_write_block);
52
53u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
54{
Mark Brown6e3ad112011-08-08 17:04:40 +090055 unsigned int val;
56 int ret;
Balaji Raof52046b2009-01-09 01:49:01 +010057
Mark Brown6e3ad112011-08-08 17:04:40 +090058 ret = regmap_read(pcf->regmap, reg, &val);
59 if (ret < 0)
60 return -1;
Balaji Raof52046b2009-01-09 01:49:01 +010061
62 return val;
63}
64EXPORT_SYMBOL_GPL(pcf50633_reg_read);
65
66int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
67{
Mark Brown6e3ad112011-08-08 17:04:40 +090068 return regmap_write(pcf->regmap, reg, val);
Balaji Raof52046b2009-01-09 01:49:01 +010069}
70EXPORT_SYMBOL_GPL(pcf50633_reg_write);
71
72int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
73{
Mark Brown6e3ad112011-08-08 17:04:40 +090074 return regmap_update_bits(pcf->regmap, reg, mask, val);
Balaji Raof52046b2009-01-09 01:49:01 +010075}
76EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
77
78int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
79{
Mark Brown6e3ad112011-08-08 17:04:40 +090080 return regmap_update_bits(pcf->regmap, reg, val, 0);
Balaji Raof52046b2009-01-09 01:49:01 +010081}
82EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
83
84/* sysfs attributes */
85static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
86 char *buf)
87{
88 struct pcf50633 *pcf = dev_get_drvdata(dev);
89 u8 dump[16];
90 int n, n1, idx = 0;
91 char *buf1 = buf;
92 static u8 address_no_read[] = { /* must be ascending */
93 PCF50633_REG_INT1,
94 PCF50633_REG_INT2,
95 PCF50633_REG_INT3,
96 PCF50633_REG_INT4,
97 PCF50633_REG_INT5,
98 0 /* terminator */
99 };
100
101 for (n = 0; n < 256; n += sizeof(dump)) {
102 for (n1 = 0; n1 < sizeof(dump); n1++)
103 if (n == address_no_read[idx]) {
104 idx++;
105 dump[n1] = 0x00;
106 } else
107 dump[n1] = pcf50633_reg_read(pcf, n + n1);
108
109 hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0);
110 buf1 += strlen(buf1);
111 *buf1++ = '\n';
112 *buf1 = '\0';
113 }
114
115 return buf1 - buf;
116}
117static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
118
119static ssize_t show_resume_reason(struct device *dev,
120 struct device_attribute *attr, char *buf)
121{
122 struct pcf50633 *pcf = dev_get_drvdata(dev);
123 int n;
124
125 n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
126 pcf->resume_reason[0],
127 pcf->resume_reason[1],
128 pcf->resume_reason[2],
129 pcf->resume_reason[3],
130 pcf->resume_reason[4]);
131
132 return n;
133}
134static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
135
136static struct attribute *pcf_sysfs_entries[] = {
137 &dev_attr_dump_regs.attr,
138 &dev_attr_resume_reason.attr,
139 NULL,
140};
141
142static struct attribute_group pcf_attr_group = {
143 .name = NULL, /* put in device directory */
144 .attrs = pcf_sysfs_entries,
145};
146
Balaji Raof52046b2009-01-09 01:49:01 +0100147static void
148pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
149 struct platform_device **pdev)
150{
Balaji Raof52046b2009-01-09 01:49:01 +0100151 int ret;
152
153 *pdev = platform_device_alloc(name, -1);
154 if (!*pdev) {
155 dev_err(pcf->dev, "Falied to allocate %s\n", name);
156 return;
157 }
158
Balaji Raof52046b2009-01-09 01:49:01 +0100159 (*pdev)->dev.parent = pcf->dev;
160
161 ret = platform_device_add(*pdev);
162 if (ret) {
163 dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
164 platform_device_put(*pdev);
165 *pdev = NULL;
166 }
167}
168
Mark Brown939941d2011-01-20 21:52:42 +0000169#ifdef CONFIG_PM_SLEEP
170static int pcf50633_suspend(struct device *dev)
Balaji Raof52046b2009-01-09 01:49:01 +0100171{
Mark Brown939941d2011-01-20 21:52:42 +0000172 struct i2c_client *client = to_i2c_client(dev);
173 struct pcf50633 *pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100174
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200175 return pcf50633_irq_suspend(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100176}
177
Mark Brown939941d2011-01-20 21:52:42 +0000178static int pcf50633_resume(struct device *dev)
Balaji Raof52046b2009-01-09 01:49:01 +0100179{
Mark Brown939941d2011-01-20 21:52:42 +0000180 struct i2c_client *client = to_i2c_client(dev);
181 struct pcf50633 *pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100182
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200183 return pcf50633_irq_resume(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100184}
Balaji Raof52046b2009-01-09 01:49:01 +0100185#endif
186
Mark Brown939941d2011-01-20 21:52:42 +0000187static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
188
Mark Brown6e3ad112011-08-08 17:04:40 +0900189static struct regmap_config pcf50633_regmap_config = {
190 .reg_bits = 8,
191 .val_bits = 8,
192};
193
Balaji Raof52046b2009-01-09 01:49:01 +0100194static int __devinit pcf50633_probe(struct i2c_client *client,
195 const struct i2c_device_id *ids)
196{
197 struct pcf50633 *pcf;
198 struct pcf50633_platform_data *pdata = client->dev.platform_data;
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200199 int i, ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100200 int version, variant;
201
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200202 if (!client->irq) {
203 dev_err(&client->dev, "Missing IRQ\n");
204 return -ENOENT;
205 }
206
Balaji Raof52046b2009-01-09 01:49:01 +0100207 pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
208 if (!pcf)
209 return -ENOMEM;
210
211 pcf->pdata = pdata;
212
213 mutex_init(&pcf->lock);
214
Mark Brown6e3ad112011-08-08 17:04:40 +0900215 pcf->regmap = regmap_init_i2c(client, &pcf50633_regmap_config);
216 if (IS_ERR(pcf->regmap)) {
217 ret = PTR_ERR(pcf->regmap);
218 dev_err(pcf->dev, "Failed to allocate register map: %d\n",
219 ret);
220 goto err_free;
221 }
222
Balaji Raof52046b2009-01-09 01:49:01 +0100223 i2c_set_clientdata(client, pcf);
224 pcf->dev = &client->dev;
Balaji Raof52046b2009-01-09 01:49:01 +0100225
226 version = pcf50633_reg_read(pcf, 0);
227 variant = pcf50633_reg_read(pcf, 1);
228 if (version < 0 || variant < 0) {
229 dev_err(pcf->dev, "Unable to probe pcf50633\n");
230 ret = -ENODEV;
Mark Brown6e3ad112011-08-08 17:04:40 +0900231 goto err_regmap;
Balaji Raof52046b2009-01-09 01:49:01 +0100232 }
233
234 dev_info(pcf->dev, "Probed device version %d variant %d\n",
235 version, variant);
236
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200237 pcf50633_irq_init(pcf, client->irq);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200238
Balaji Raof52046b2009-01-09 01:49:01 +0100239 /* Create sub devices */
240 pcf50633_client_dev_register(pcf, "pcf50633-input",
241 &pcf->input_pdev);
242 pcf50633_client_dev_register(pcf, "pcf50633-rtc",
243 &pcf->rtc_pdev);
244 pcf50633_client_dev_register(pcf, "pcf50633-mbc",
245 &pcf->mbc_pdev);
246 pcf50633_client_dev_register(pcf, "pcf50633-adc",
247 &pcf->adc_pdev);
Lars-Peter Clausenf5bf4032010-05-12 02:44:33 +0200248 pcf50633_client_dev_register(pcf, "pcf50633-backlight",
249 &pcf->bl_pdev);
250
Balaji Raof52046b2009-01-09 01:49:01 +0100251
252 for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
253 struct platform_device *pdev;
254
255 pdev = platform_device_alloc("pcf50633-regltr", i);
256 if (!pdev) {
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200257 dev_err(pcf->dev, "Cannot create regulator %d\n", i);
Balaji Raof52046b2009-01-09 01:49:01 +0100258 continue;
259 }
260
261 pdev->dev.parent = pcf->dev;
Lars-Peter Clausenbbb2e492009-10-14 02:12:35 +0400262 platform_device_add_data(pdev, &pdata->reg_init_data[i],
263 sizeof(pdata->reg_init_data[i]));
Balaji Raof52046b2009-01-09 01:49:01 +0100264 pcf->regulator_pdev[i] = pdev;
265
266 platform_device_add(pdev);
267 }
268
Balaji Raof52046b2009-01-09 01:49:01 +0100269 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
270 if (ret)
271 dev_err(pcf->dev, "error creating sysfs entries\n");
272
273 if (pdata->probe_done)
274 pdata->probe_done(pcf);
275
276 return 0;
277
Mark Brown6e3ad112011-08-08 17:04:40 +0900278err_regmap:
279 regmap_exit(pcf->regmap);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200280err_free:
Balaji Raof52046b2009-01-09 01:49:01 +0100281 kfree(pcf);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200282
Balaji Raof52046b2009-01-09 01:49:01 +0100283 return ret;
284}
285
286static int __devexit pcf50633_remove(struct i2c_client *client)
287{
288 struct pcf50633 *pcf = i2c_get_clientdata(client);
289 int i;
290
Axel Lin8220fe42010-10-20 16:56:59 +0800291 sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200292 pcf50633_irq_free(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100293
294 platform_device_unregister(pcf->input_pdev);
295 platform_device_unregister(pcf->rtc_pdev);
296 platform_device_unregister(pcf->mbc_pdev);
297 platform_device_unregister(pcf->adc_pdev);
Axel Lin8220fe42010-10-20 16:56:59 +0800298 platform_device_unregister(pcf->bl_pdev);
Balaji Raof52046b2009-01-09 01:49:01 +0100299
300 for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
301 platform_device_unregister(pcf->regulator_pdev[i]);
302
Mark Brown6e3ad112011-08-08 17:04:40 +0900303 regmap_exit(pcf->regmap);
Balaji Raof52046b2009-01-09 01:49:01 +0100304 kfree(pcf);
305
306 return 0;
307}
308
Axel Lin12065522011-03-23 20:54:17 +0800309static const struct i2c_device_id pcf50633_id_table[] = {
Balaji Raof52046b2009-01-09 01:49:01 +0100310 {"pcf50633", 0x73},
Jean Delvare8915e542009-02-17 09:07:02 +0100311 {/* end of list */}
Balaji Raof52046b2009-01-09 01:49:01 +0100312};
Axel Lin76790892011-02-28 14:33:12 +0800313MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
Balaji Raof52046b2009-01-09 01:49:01 +0100314
315static struct i2c_driver pcf50633_driver = {
316 .driver = {
317 .name = "pcf50633",
Mark Brown939941d2011-01-20 21:52:42 +0000318 .pm = &pcf50633_pm,
Balaji Raof52046b2009-01-09 01:49:01 +0100319 },
320 .id_table = pcf50633_id_table,
321 .probe = pcf50633_probe,
322 .remove = __devexit_p(pcf50633_remove),
323};
324
325static int __init pcf50633_init(void)
326{
327 return i2c_add_driver(&pcf50633_driver);
328}
329
330static void __exit pcf50633_exit(void)
331{
332 i2c_del_driver(&pcf50633_driver);
333}
334
335MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
336MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
337MODULE_LICENSE("GPL");
338
Samuel Ortiz2021de82009-06-15 18:04:54 +0200339subsys_initcall(pcf50633_init);
Balaji Raof52046b2009-01-09 01:49:01 +0100340module_exit(pcf50633_exit);