blob: 6155d123a84e762c3491674c12200e165c21ec94 [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
Andy Shevchenko970d9fbc2014-09-04 12:32:12 +0300109 buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
Balaji Raof52046b2009-01-09 01:49:01 +0100110 }
111
112 return buf1 - buf;
113}
114static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
115
116static ssize_t show_resume_reason(struct device *dev,
117 struct device_attribute *attr, char *buf)
118{
119 struct pcf50633 *pcf = dev_get_drvdata(dev);
120 int n;
121
122 n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
123 pcf->resume_reason[0],
124 pcf->resume_reason[1],
125 pcf->resume_reason[2],
126 pcf->resume_reason[3],
127 pcf->resume_reason[4]);
128
129 return n;
130}
131static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
132
133static struct attribute *pcf_sysfs_entries[] = {
134 &dev_attr_dump_regs.attr,
135 &dev_attr_resume_reason.attr,
136 NULL,
137};
138
139static struct attribute_group pcf_attr_group = {
140 .name = NULL, /* put in device directory */
141 .attrs = pcf_sysfs_entries,
142};
143
Balaji Raof52046b2009-01-09 01:49:01 +0100144static void
145pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
146 struct platform_device **pdev)
147{
Balaji Raof52046b2009-01-09 01:49:01 +0100148 int ret;
149
150 *pdev = platform_device_alloc(name, -1);
151 if (!*pdev) {
152 dev_err(pcf->dev, "Falied to allocate %s\n", name);
153 return;
154 }
155
Balaji Raof52046b2009-01-09 01:49:01 +0100156 (*pdev)->dev.parent = pcf->dev;
157
158 ret = platform_device_add(*pdev);
159 if (ret) {
160 dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
161 platform_device_put(*pdev);
162 *pdev = NULL;
163 }
164}
165
Mark Brown939941d2011-01-20 21:52:42 +0000166#ifdef CONFIG_PM_SLEEP
167static int pcf50633_suspend(struct device *dev)
Balaji Raof52046b2009-01-09 01:49:01 +0100168{
Mark Brown939941d2011-01-20 21:52:42 +0000169 struct i2c_client *client = to_i2c_client(dev);
170 struct pcf50633 *pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100171
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200172 return pcf50633_irq_suspend(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100173}
174
Mark Brown939941d2011-01-20 21:52:42 +0000175static int pcf50633_resume(struct device *dev)
Balaji Raof52046b2009-01-09 01:49:01 +0100176{
Mark Brown939941d2011-01-20 21:52:42 +0000177 struct i2c_client *client = to_i2c_client(dev);
178 struct pcf50633 *pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100179
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200180 return pcf50633_irq_resume(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100181}
Balaji Raof52046b2009-01-09 01:49:01 +0100182#endif
183
Mark Brown939941d2011-01-20 21:52:42 +0000184static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
185
Krzysztof Kozlowski1590d4a2015-01-05 10:01:27 +0100186static const struct regmap_config pcf50633_regmap_config = {
Mark Brown6e3ad112011-08-08 17:04:40 +0900187 .reg_bits = 8,
188 .val_bits = 8,
189};
190
Bill Pembertonf791be42012-11-19 13:23:04 -0500191static int pcf50633_probe(struct i2c_client *client,
Balaji Raof52046b2009-01-09 01:49:01 +0100192 const struct i2c_device_id *ids)
193{
194 struct pcf50633 *pcf;
Lee Jonescddc1142014-08-18 15:54:06 +0100195 struct platform_device *pdev;
Jingoo Han334a41ce2013-07-30 17:10:05 +0900196 struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
Lee Jonescddc1142014-08-18 15:54:06 +0100197 int i, j, ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100198 int version, variant;
199
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200200 if (!client->irq) {
201 dev_err(&client->dev, "Missing IRQ\n");
202 return -ENOENT;
203 }
204
Axel Linaa4603a2012-05-11 09:31:29 +0800205 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
Balaji Raof52046b2009-01-09 01:49:01 +0100206 if (!pcf)
207 return -ENOMEM;
208
Axel Linb30dd8f2012-12-25 10:52:49 +0800209 i2c_set_clientdata(client, pcf);
210 pcf->dev = &client->dev;
Balaji Raof52046b2009-01-09 01:49:01 +0100211 pcf->pdata = pdata;
212
213 mutex_init(&pcf->lock);
214
Axel Linaa4603a2012-05-11 09:31:29 +0800215 pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
Mark Brown6e3ad112011-08-08 17:04:40 +0900216 if (IS_ERR(pcf->regmap)) {
217 ret = PTR_ERR(pcf->regmap);
Axel Linaa4603a2012-05-11 09:31:29 +0800218 dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
219 return ret;
Mark Brown6e3ad112011-08-08 17:04:40 +0900220 }
221
Balaji Raof52046b2009-01-09 01:49:01 +0100222 version = pcf50633_reg_read(pcf, 0);
223 variant = pcf50633_reg_read(pcf, 1);
224 if (version < 0 || variant < 0) {
225 dev_err(pcf->dev, "Unable to probe pcf50633\n");
226 ret = -ENODEV;
Axel Linaa4603a2012-05-11 09:31:29 +0800227 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100228 }
229
230 dev_info(pcf->dev, "Probed device version %d variant %d\n",
231 version, variant);
232
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200233 pcf50633_irq_init(pcf, client->irq);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200234
Balaji Raof52046b2009-01-09 01:49:01 +0100235 /* Create sub devices */
Axel Linaa4603a2012-05-11 09:31:29 +0800236 pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
237 pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
238 pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
239 pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
240 pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
Lars-Peter Clausenf5bf4032010-05-12 02:44:33 +0200241
Balaji Raof52046b2009-01-09 01:49:01 +0100242
243 for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
Axel Lin561427f2013-11-30 12:21:03 +0800244 pdev = platform_device_alloc("pcf50633-regulator", i);
Lee Jonesc9810152014-07-01 12:57:36 +0100245 if (!pdev)
246 return -ENOMEM;
Balaji Raof52046b2009-01-09 01:49:01 +0100247
248 pdev->dev.parent = pcf->dev;
Lee Jonesc9810152014-07-01 12:57:36 +0100249 ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
250 sizeof(pdata->reg_init_data[i]));
Lee Jonescddc1142014-08-18 15:54:06 +0100251 if (ret)
252 goto err;
Balaji Raof52046b2009-01-09 01:49:01 +0100253
Lee Jonescddc1142014-08-18 15:54:06 +0100254 ret = platform_device_add(pdev);
255 if (ret)
256 goto err;
257
258 pcf->regulator_pdev[i] = pdev;
Balaji Raof52046b2009-01-09 01:49:01 +0100259 }
260
Balaji Raof52046b2009-01-09 01:49:01 +0100261 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
262 if (ret)
Lee Jonescddc1142014-08-18 15:54:06 +0100263 dev_warn(pcf->dev, "error creating sysfs entries\n");
Balaji Raof52046b2009-01-09 01:49:01 +0100264
265 if (pdata->probe_done)
266 pdata->probe_done(pcf);
267
268 return 0;
Lee Jonescddc1142014-08-18 15:54:06 +0100269
270err:
271 platform_device_put(pdev);
272 for (j = 0; j < i; j++)
273 platform_device_put(pcf->regulator_pdev[j]);
274
275 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100276}
277
Bill Pemberton4740f732012-11-19 13:26:01 -0500278static int pcf50633_remove(struct i2c_client *client)
Balaji Raof52046b2009-01-09 01:49:01 +0100279{
280 struct pcf50633 *pcf = i2c_get_clientdata(client);
281 int i;
282
Axel Lin8220fe42010-10-20 16:56:59 +0800283 sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200284 pcf50633_irq_free(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100285
286 platform_device_unregister(pcf->input_pdev);
287 platform_device_unregister(pcf->rtc_pdev);
288 platform_device_unregister(pcf->mbc_pdev);
289 platform_device_unregister(pcf->adc_pdev);
Axel Lin8220fe42010-10-20 16:56:59 +0800290 platform_device_unregister(pcf->bl_pdev);
Balaji Raof52046b2009-01-09 01:49:01 +0100291
292 for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
293 platform_device_unregister(pcf->regulator_pdev[i]);
294
Balaji Raof52046b2009-01-09 01:49:01 +0100295 return 0;
296}
297
Axel Lin12065522011-03-23 20:54:17 +0800298static const struct i2c_device_id pcf50633_id_table[] = {
Balaji Raof52046b2009-01-09 01:49:01 +0100299 {"pcf50633", 0x73},
Jean Delvare8915e542009-02-17 09:07:02 +0100300 {/* end of list */}
Balaji Raof52046b2009-01-09 01:49:01 +0100301};
Axel Lin76790892011-02-28 14:33:12 +0800302MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
Balaji Raof52046b2009-01-09 01:49:01 +0100303
304static struct i2c_driver pcf50633_driver = {
305 .driver = {
306 .name = "pcf50633",
Mark Brown939941d2011-01-20 21:52:42 +0000307 .pm = &pcf50633_pm,
Balaji Raof52046b2009-01-09 01:49:01 +0100308 },
309 .id_table = pcf50633_id_table,
310 .probe = pcf50633_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500311 .remove = pcf50633_remove,
Balaji Raof52046b2009-01-09 01:49:01 +0100312};
313
314static int __init pcf50633_init(void)
315{
316 return i2c_add_driver(&pcf50633_driver);
317}
318
319static void __exit pcf50633_exit(void)
320{
321 i2c_del_driver(&pcf50633_driver);
322}
323
324MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
325MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
326MODULE_LICENSE("GPL");
327
Samuel Ortiz2021de82009-06-15 18:04:54 +0200328subsys_initcall(pcf50633_init);
Balaji Raof52046b2009-01-09 01:49:01 +0100329module_exit(pcf50633_exit);