blob: 501ce13b693ea8b105d9fe1c06d4be7e27140943 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Balaji Raof52046b2009-01-09 01:49:01 +010025
26#include <linux/mfd/pcf50633/core.h>
27
Balaji Raof52046b2009-01-09 01:49:01 +010028static int __pcf50633_read(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
29{
30 int ret;
31
32 ret = i2c_smbus_read_i2c_block_data(pcf->i2c_client, reg,
33 num, data);
34 if (ret < 0)
35 dev_err(pcf->dev, "Error reading %d regs at %d\n", num, reg);
36
37 return ret;
38}
39
40static int __pcf50633_write(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
41{
42 int ret;
43
44 ret = i2c_smbus_write_i2c_block_data(pcf->i2c_client, reg,
45 num, data);
46 if (ret < 0)
47 dev_err(pcf->dev, "Error writing %d regs at %d\n", num, reg);
48
49 return ret;
50
51}
52
53/* Read a block of upto 32 regs */
54int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
55 int nr_regs, u8 *data)
56{
57 int ret;
58
59 mutex_lock(&pcf->lock);
60 ret = __pcf50633_read(pcf, reg, nr_regs, data);
61 mutex_unlock(&pcf->lock);
62
63 return ret;
64}
65EXPORT_SYMBOL_GPL(pcf50633_read_block);
66
67/* Write a block of upto 32 regs */
68int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
69 int nr_regs, u8 *data)
70{
71 int ret;
72
73 mutex_lock(&pcf->lock);
74 ret = __pcf50633_write(pcf, reg, nr_regs, data);
75 mutex_unlock(&pcf->lock);
76
77 return ret;
78}
79EXPORT_SYMBOL_GPL(pcf50633_write_block);
80
81u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
82{
83 u8 val;
84
85 mutex_lock(&pcf->lock);
86 __pcf50633_read(pcf, reg, 1, &val);
87 mutex_unlock(&pcf->lock);
88
89 return val;
90}
91EXPORT_SYMBOL_GPL(pcf50633_reg_read);
92
93int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
94{
95 int ret;
96
97 mutex_lock(&pcf->lock);
98 ret = __pcf50633_write(pcf, reg, 1, &val);
99 mutex_unlock(&pcf->lock);
100
101 return ret;
102}
103EXPORT_SYMBOL_GPL(pcf50633_reg_write);
104
105int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
106{
107 int ret;
108 u8 tmp;
109
110 val &= mask;
111
112 mutex_lock(&pcf->lock);
113 ret = __pcf50633_read(pcf, reg, 1, &tmp);
114 if (ret < 0)
115 goto out;
116
117 tmp &= ~mask;
118 tmp |= val;
119 ret = __pcf50633_write(pcf, reg, 1, &tmp);
120
121out:
122 mutex_unlock(&pcf->lock);
123
124 return ret;
125}
126EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
127
128int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
129{
130 int ret;
131 u8 tmp;
132
133 mutex_lock(&pcf->lock);
134 ret = __pcf50633_read(pcf, reg, 1, &tmp);
135 if (ret < 0)
136 goto out;
137
138 tmp &= ~val;
139 ret = __pcf50633_write(pcf, reg, 1, &tmp);
140
141out:
142 mutex_unlock(&pcf->lock);
143
144 return ret;
145}
146EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
147
148/* sysfs attributes */
149static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
150 char *buf)
151{
152 struct pcf50633 *pcf = dev_get_drvdata(dev);
153 u8 dump[16];
154 int n, n1, idx = 0;
155 char *buf1 = buf;
156 static u8 address_no_read[] = { /* must be ascending */
157 PCF50633_REG_INT1,
158 PCF50633_REG_INT2,
159 PCF50633_REG_INT3,
160 PCF50633_REG_INT4,
161 PCF50633_REG_INT5,
162 0 /* terminator */
163 };
164
165 for (n = 0; n < 256; n += sizeof(dump)) {
166 for (n1 = 0; n1 < sizeof(dump); n1++)
167 if (n == address_no_read[idx]) {
168 idx++;
169 dump[n1] = 0x00;
170 } else
171 dump[n1] = pcf50633_reg_read(pcf, n + n1);
172
173 hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0);
174 buf1 += strlen(buf1);
175 *buf1++ = '\n';
176 *buf1 = '\0';
177 }
178
179 return buf1 - buf;
180}
181static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
182
183static ssize_t show_resume_reason(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
186 struct pcf50633 *pcf = dev_get_drvdata(dev);
187 int n;
188
189 n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
190 pcf->resume_reason[0],
191 pcf->resume_reason[1],
192 pcf->resume_reason[2],
193 pcf->resume_reason[3],
194 pcf->resume_reason[4]);
195
196 return n;
197}
198static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
199
200static struct attribute *pcf_sysfs_entries[] = {
201 &dev_attr_dump_regs.attr,
202 &dev_attr_resume_reason.attr,
203 NULL,
204};
205
206static struct attribute_group pcf_attr_group = {
207 .name = NULL, /* put in device directory */
208 .attrs = pcf_sysfs_entries,
209};
210
Balaji Raof52046b2009-01-09 01:49:01 +0100211static void
212pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
213 struct platform_device **pdev)
214{
Balaji Raof52046b2009-01-09 01:49:01 +0100215 int ret;
216
217 *pdev = platform_device_alloc(name, -1);
218 if (!*pdev) {
219 dev_err(pcf->dev, "Falied to allocate %s\n", name);
220 return;
221 }
222
Balaji Raof52046b2009-01-09 01:49:01 +0100223 (*pdev)->dev.parent = pcf->dev;
224
225 ret = platform_device_add(*pdev);
226 if (ret) {
227 dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
228 platform_device_put(*pdev);
229 *pdev = NULL;
230 }
231}
232
233#ifdef CONFIG_PM
Lars-Peter Clausen25993e42009-10-14 02:12:31 +0400234static int pcf50633_suspend(struct i2c_client *client, pm_message_t state)
Balaji Raof52046b2009-01-09 01:49:01 +0100235{
236 struct pcf50633 *pcf;
Lars-Peter Clausen25993e42009-10-14 02:12:31 +0400237 pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100238
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200239 return pcf50633_irq_suspend(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100240}
241
Lars-Peter Clausen25993e42009-10-14 02:12:31 +0400242static int pcf50633_resume(struct i2c_client *client)
Balaji Raof52046b2009-01-09 01:49:01 +0100243{
244 struct pcf50633 *pcf;
Lars-Peter Clausen25993e42009-10-14 02:12:31 +0400245 pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100246
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200247 return pcf50633_irq_resume(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100248}
249#else
250#define pcf50633_suspend NULL
251#define pcf50633_resume NULL
252#endif
253
254static int __devinit pcf50633_probe(struct i2c_client *client,
255 const struct i2c_device_id *ids)
256{
257 struct pcf50633 *pcf;
258 struct pcf50633_platform_data *pdata = client->dev.platform_data;
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200259 int i, ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100260 int version, variant;
261
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200262 if (!client->irq) {
263 dev_err(&client->dev, "Missing IRQ\n");
264 return -ENOENT;
265 }
266
Balaji Raof52046b2009-01-09 01:49:01 +0100267 pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
268 if (!pcf)
269 return -ENOMEM;
270
271 pcf->pdata = pdata;
272
273 mutex_init(&pcf->lock);
274
275 i2c_set_clientdata(client, pcf);
276 pcf->dev = &client->dev;
277 pcf->i2c_client = client;
Balaji Raof52046b2009-01-09 01:49:01 +0100278
279 version = pcf50633_reg_read(pcf, 0);
280 variant = pcf50633_reg_read(pcf, 1);
281 if (version < 0 || variant < 0) {
282 dev_err(pcf->dev, "Unable to probe pcf50633\n");
283 ret = -ENODEV;
Lars-Peter Clausenf7b2a772010-05-12 02:10:55 +0200284 goto err_free;
Balaji Raof52046b2009-01-09 01:49:01 +0100285 }
286
287 dev_info(pcf->dev, "Probed device version %d variant %d\n",
288 version, variant);
289
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200290 pcf50633_irq_init(pcf, client->irq);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200291
Balaji Raof52046b2009-01-09 01:49:01 +0100292 /* Create sub devices */
293 pcf50633_client_dev_register(pcf, "pcf50633-input",
294 &pcf->input_pdev);
295 pcf50633_client_dev_register(pcf, "pcf50633-rtc",
296 &pcf->rtc_pdev);
297 pcf50633_client_dev_register(pcf, "pcf50633-mbc",
298 &pcf->mbc_pdev);
299 pcf50633_client_dev_register(pcf, "pcf50633-adc",
300 &pcf->adc_pdev);
Lars-Peter Clausenf5bf4032010-05-12 02:44:33 +0200301 pcf50633_client_dev_register(pcf, "pcf50633-backlight",
302 &pcf->bl_pdev);
303
Balaji Raof52046b2009-01-09 01:49:01 +0100304
305 for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
306 struct platform_device *pdev;
307
308 pdev = platform_device_alloc("pcf50633-regltr", i);
309 if (!pdev) {
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200310 dev_err(pcf->dev, "Cannot create regulator %d\n", i);
Balaji Raof52046b2009-01-09 01:49:01 +0100311 continue;
312 }
313
314 pdev->dev.parent = pcf->dev;
Lars-Peter Clausenbbb2e492009-10-14 02:12:35 +0400315 platform_device_add_data(pdev, &pdata->reg_init_data[i],
316 sizeof(pdata->reg_init_data[i]));
Balaji Raof52046b2009-01-09 01:49:01 +0100317 pcf->regulator_pdev[i] = pdev;
318
319 platform_device_add(pdev);
320 }
321
Balaji Raof52046b2009-01-09 01:49:01 +0100322 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
323 if (ret)
324 dev_err(pcf->dev, "error creating sysfs entries\n");
325
326 if (pdata->probe_done)
327 pdata->probe_done(pcf);
328
329 return 0;
330
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200331err_free:
Balaji Raof52046b2009-01-09 01:49:01 +0100332 kfree(pcf);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200333
Balaji Raof52046b2009-01-09 01:49:01 +0100334 return ret;
335}
336
337static int __devexit pcf50633_remove(struct i2c_client *client)
338{
339 struct pcf50633 *pcf = i2c_get_clientdata(client);
340 int i;
341
Axel Lin8220fe42010-10-20 16:56:59 +0800342 sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200343 pcf50633_irq_free(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100344
345 platform_device_unregister(pcf->input_pdev);
346 platform_device_unregister(pcf->rtc_pdev);
347 platform_device_unregister(pcf->mbc_pdev);
348 platform_device_unregister(pcf->adc_pdev);
Axel Lin8220fe42010-10-20 16:56:59 +0800349 platform_device_unregister(pcf->bl_pdev);
Balaji Raof52046b2009-01-09 01:49:01 +0100350
351 for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
352 platform_device_unregister(pcf->regulator_pdev[i]);
353
354 kfree(pcf);
355
356 return 0;
357}
358
359static struct i2c_device_id pcf50633_id_table[] = {
360 {"pcf50633", 0x73},
Jean Delvare8915e542009-02-17 09:07:02 +0100361 {/* end of list */}
Balaji Raof52046b2009-01-09 01:49:01 +0100362};
363
364static struct i2c_driver pcf50633_driver = {
365 .driver = {
366 .name = "pcf50633",
Balaji Raof52046b2009-01-09 01:49:01 +0100367 },
368 .id_table = pcf50633_id_table,
369 .probe = pcf50633_probe,
370 .remove = __devexit_p(pcf50633_remove),
Lars-Peter Clausen25993e42009-10-14 02:12:31 +0400371 .suspend = pcf50633_suspend,
372 .resume = pcf50633_resume,
Balaji Raof52046b2009-01-09 01:49:01 +0100373};
374
375static int __init pcf50633_init(void)
376{
377 return i2c_add_driver(&pcf50633_driver);
378}
379
380static void __exit pcf50633_exit(void)
381{
382 i2c_del_driver(&pcf50633_driver);
383}
384
385MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
386MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
387MODULE_LICENSE("GPL");
388
Samuel Ortiz2021de82009-06-15 18:04:54 +0200389subsys_initcall(pcf50633_init);
Balaji Raof52046b2009-01-09 01:49:01 +0100390module_exit(pcf50633_exit);