blob: 148bcd6120f45a396a2f2abac082efb443eefae9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Balaji Raof52046b2009-01-09 01:49:01 +01002/* NXP PCF50633 Power Management Unit (PMU) driver
3 *
4 * (C) 2006-2008 by Openmoko, Inc.
5 * Author: Harald Welte <laforge@openmoko.org>
6 * Balaji Rao <balajirrao@openmoko.org>
7 * All rights reserved.
Balaji Raof52046b2009-01-09 01:49:01 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/sysfs.h>
Balaji Raof52046b2009-01-09 01:49:01 +010013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/interrupt.h>
16#include <linux/workqueue.h>
17#include <linux/platform_device.h>
18#include <linux/i2c.h>
Mark Brown939941d2011-01-20 21:52:42 +000019#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brown6e3ad112011-08-08 17:04:40 +090021#include <linux/regmap.h>
22#include <linux/err.h>
Balaji Raof52046b2009-01-09 01:49:01 +010023
24#include <linux/mfd/pcf50633/core.h>
25
Lucas De Marchi25985ed2011-03-30 22:57:33 -030026/* Read a block of up to 32 regs */
Balaji Raof52046b2009-01-09 01:49:01 +010027int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
28 int nr_regs, u8 *data)
29{
30 int ret;
31
Mark Brown6e3ad112011-08-08 17:04:40 +090032 ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
33 if (ret != 0)
34 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +010035
Mark Brown6e3ad112011-08-08 17:04:40 +090036 return nr_regs;
Balaji Raof52046b2009-01-09 01:49:01 +010037}
38EXPORT_SYMBOL_GPL(pcf50633_read_block);
39
Lucas De Marchi25985ed2011-03-30 22:57:33 -030040/* Write a block of up to 32 regs */
Balaji Raof52046b2009-01-09 01:49:01 +010041int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
42 int nr_regs, u8 *data)
43{
Axel Lin60b5c5a42012-02-14 11:30:31 +080044 return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
Balaji Raof52046b2009-01-09 01:49:01 +010045}
46EXPORT_SYMBOL_GPL(pcf50633_write_block);
47
48u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
49{
Mark Brown6e3ad112011-08-08 17:04:40 +090050 unsigned int val;
51 int ret;
Balaji Raof52046b2009-01-09 01:49:01 +010052
Mark Brown6e3ad112011-08-08 17:04:40 +090053 ret = regmap_read(pcf->regmap, reg, &val);
54 if (ret < 0)
55 return -1;
Balaji Raof52046b2009-01-09 01:49:01 +010056
57 return val;
58}
59EXPORT_SYMBOL_GPL(pcf50633_reg_read);
60
61int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
62{
Mark Brown6e3ad112011-08-08 17:04:40 +090063 return regmap_write(pcf->regmap, reg, val);
Balaji Raof52046b2009-01-09 01:49:01 +010064}
65EXPORT_SYMBOL_GPL(pcf50633_reg_write);
66
67int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
68{
Mark Brown6e3ad112011-08-08 17:04:40 +090069 return regmap_update_bits(pcf->regmap, reg, mask, val);
Balaji Raof52046b2009-01-09 01:49:01 +010070}
71EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
72
73int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
74{
Mark Brown6e3ad112011-08-08 17:04:40 +090075 return regmap_update_bits(pcf->regmap, reg, val, 0);
Balaji Raof52046b2009-01-09 01:49:01 +010076}
77EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
78
79/* sysfs attributes */
80static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
81 char *buf)
82{
83 struct pcf50633 *pcf = dev_get_drvdata(dev);
84 u8 dump[16];
85 int n, n1, idx = 0;
86 char *buf1 = buf;
87 static u8 address_no_read[] = { /* must be ascending */
88 PCF50633_REG_INT1,
89 PCF50633_REG_INT2,
90 PCF50633_REG_INT3,
91 PCF50633_REG_INT4,
92 PCF50633_REG_INT5,
93 0 /* terminator */
94 };
95
96 for (n = 0; n < 256; n += sizeof(dump)) {
97 for (n1 = 0; n1 < sizeof(dump); n1++)
98 if (n == address_no_read[idx]) {
99 idx++;
100 dump[n1] = 0x00;
101 } else
102 dump[n1] = pcf50633_reg_read(pcf, n + n1);
103
Andy Shevchenko970d9fb2014-09-04 12:32:12 +0300104 buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
Balaji Raof52046b2009-01-09 01:49:01 +0100105 }
106
107 return buf1 - buf;
108}
109static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
110
111static ssize_t show_resume_reason(struct device *dev,
112 struct device_attribute *attr, char *buf)
113{
114 struct pcf50633 *pcf = dev_get_drvdata(dev);
115 int n;
116
117 n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
118 pcf->resume_reason[0],
119 pcf->resume_reason[1],
120 pcf->resume_reason[2],
121 pcf->resume_reason[3],
122 pcf->resume_reason[4]);
123
124 return n;
125}
126static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
127
128static struct attribute *pcf_sysfs_entries[] = {
129 &dev_attr_dump_regs.attr,
130 &dev_attr_resume_reason.attr,
131 NULL,
132};
133
134static struct attribute_group pcf_attr_group = {
135 .name = NULL, /* put in device directory */
136 .attrs = pcf_sysfs_entries,
137};
138
Balaji Raof52046b2009-01-09 01:49:01 +0100139static void
140pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
141 struct platform_device **pdev)
142{
Balaji Raof52046b2009-01-09 01:49:01 +0100143 int ret;
144
145 *pdev = platform_device_alloc(name, -1);
146 if (!*pdev) {
Arvind Yadavf058aa32017-10-29 11:31:23 +0530147 dev_err(pcf->dev, "Failed to allocate %s\n", name);
Balaji Raof52046b2009-01-09 01:49:01 +0100148 return;
149 }
150
Balaji Raof52046b2009-01-09 01:49:01 +0100151 (*pdev)->dev.parent = pcf->dev;
152
153 ret = platform_device_add(*pdev);
154 if (ret) {
155 dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
156 platform_device_put(*pdev);
157 *pdev = NULL;
158 }
159}
160
Mark Brown939941d2011-01-20 21:52:42 +0000161#ifdef CONFIG_PM_SLEEP
162static int pcf50633_suspend(struct device *dev)
Balaji Raof52046b2009-01-09 01:49:01 +0100163{
Mark Brown939941d2011-01-20 21:52:42 +0000164 struct i2c_client *client = to_i2c_client(dev);
165 struct pcf50633 *pcf = i2c_get_clientdata(client);
Balaji Raof52046b2009-01-09 01:49:01 +0100166
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200167 return pcf50633_irq_suspend(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100168}
169
Mark Brown939941d2011-01-20 21:52:42 +0000170static int pcf50633_resume(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_resume(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100176}
Balaji Raof52046b2009-01-09 01:49:01 +0100177#endif
178
Mark Brown939941d2011-01-20 21:52:42 +0000179static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
180
Krzysztof Kozlowski1590d4a2015-01-05 10:01:27 +0100181static const struct regmap_config pcf50633_regmap_config = {
Mark Brown6e3ad112011-08-08 17:04:40 +0900182 .reg_bits = 8,
183 .val_bits = 8,
184};
185
Bill Pembertonf791be42012-11-19 13:23:04 -0500186static int pcf50633_probe(struct i2c_client *client,
Balaji Raof52046b2009-01-09 01:49:01 +0100187 const struct i2c_device_id *ids)
188{
189 struct pcf50633 *pcf;
Lee Jonescddc1142014-08-18 15:54:06 +0100190 struct platform_device *pdev;
Jingoo Han334a41ce2013-07-30 17:10:05 +0900191 struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
Lee Jonescddc1142014-08-18 15:54:06 +0100192 int i, j, ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100193 int version, variant;
194
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200195 if (!client->irq) {
196 dev_err(&client->dev, "Missing IRQ\n");
197 return -ENOENT;
198 }
199
Axel Linaa4603a2012-05-11 09:31:29 +0800200 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
Balaji Raof52046b2009-01-09 01:49:01 +0100201 if (!pcf)
202 return -ENOMEM;
203
Axel Linb30dd8f2012-12-25 10:52:49 +0800204 i2c_set_clientdata(client, pcf);
205 pcf->dev = &client->dev;
Balaji Raof52046b2009-01-09 01:49:01 +0100206 pcf->pdata = pdata;
207
208 mutex_init(&pcf->lock);
209
Axel Linaa4603a2012-05-11 09:31:29 +0800210 pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
Mark Brown6e3ad112011-08-08 17:04:40 +0900211 if (IS_ERR(pcf->regmap)) {
212 ret = PTR_ERR(pcf->regmap);
Axel Linaa4603a2012-05-11 09:31:29 +0800213 dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
214 return ret;
Mark Brown6e3ad112011-08-08 17:04:40 +0900215 }
216
Balaji Raof52046b2009-01-09 01:49:01 +0100217 version = pcf50633_reg_read(pcf, 0);
218 variant = pcf50633_reg_read(pcf, 1);
219 if (version < 0 || variant < 0) {
220 dev_err(pcf->dev, "Unable to probe pcf50633\n");
221 ret = -ENODEV;
Axel Linaa4603a2012-05-11 09:31:29 +0800222 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100223 }
224
225 dev_info(pcf->dev, "Probed device version %d variant %d\n",
226 version, variant);
227
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200228 pcf50633_irq_init(pcf, client->irq);
Lars-Peter Clausen24213ae2009-10-08 00:24:54 +0200229
Balaji Raof52046b2009-01-09 01:49:01 +0100230 /* Create sub devices */
Axel Linaa4603a2012-05-11 09:31:29 +0800231 pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
232 pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
233 pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
234 pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
235 pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
Lars-Peter Clausenf5bf4032010-05-12 02:44:33 +0200236
Balaji Raof52046b2009-01-09 01:49:01 +0100237
238 for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
Axel Lin561427f2013-11-30 12:21:03 +0800239 pdev = platform_device_alloc("pcf50633-regulator", i);
Christophe JAILLET52a3a372018-03-14 22:47:55 +0100240 if (!pdev) {
241 ret = -ENOMEM;
242 goto err2;
243 }
Balaji Raof52046b2009-01-09 01:49:01 +0100244
245 pdev->dev.parent = pcf->dev;
Lee Jonesc9810152014-07-01 12:57:36 +0100246 ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
247 sizeof(pdata->reg_init_data[i]));
Lee Jonescddc1142014-08-18 15:54:06 +0100248 if (ret)
249 goto err;
Balaji Raof52046b2009-01-09 01:49:01 +0100250
Lee Jonescddc1142014-08-18 15:54:06 +0100251 ret = platform_device_add(pdev);
252 if (ret)
253 goto err;
254
255 pcf->regulator_pdev[i] = pdev;
Balaji Raof52046b2009-01-09 01:49:01 +0100256 }
257
Balaji Raof52046b2009-01-09 01:49:01 +0100258 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
259 if (ret)
Lee Jonescddc1142014-08-18 15:54:06 +0100260 dev_warn(pcf->dev, "error creating sysfs entries\n");
Balaji Raof52046b2009-01-09 01:49:01 +0100261
262 if (pdata->probe_done)
263 pdata->probe_done(pcf);
264
265 return 0;
Lee Jonescddc1142014-08-18 15:54:06 +0100266
267err:
268 platform_device_put(pdev);
Christophe JAILLET52a3a372018-03-14 22:47:55 +0100269err2:
Lee Jonescddc1142014-08-18 15:54:06 +0100270 for (j = 0; j < i; j++)
271 platform_device_put(pcf->regulator_pdev[j]);
272
273 return ret;
Balaji Raof52046b2009-01-09 01:49:01 +0100274}
275
Bill Pemberton4740f732012-11-19 13:26:01 -0500276static int pcf50633_remove(struct i2c_client *client)
Balaji Raof52046b2009-01-09 01:49:01 +0100277{
278 struct pcf50633 *pcf = i2c_get_clientdata(client);
279 int i;
280
Axel Lin8220fe42010-10-20 16:56:59 +0800281 sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
Lars-Peter Clausen380c09f2010-05-12 02:10:56 +0200282 pcf50633_irq_free(pcf);
Balaji Raof52046b2009-01-09 01:49:01 +0100283
284 platform_device_unregister(pcf->input_pdev);
285 platform_device_unregister(pcf->rtc_pdev);
286 platform_device_unregister(pcf->mbc_pdev);
287 platform_device_unregister(pcf->adc_pdev);
Axel Lin8220fe42010-10-20 16:56:59 +0800288 platform_device_unregister(pcf->bl_pdev);
Balaji Raof52046b2009-01-09 01:49:01 +0100289
290 for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
291 platform_device_unregister(pcf->regulator_pdev[i]);
292
Balaji Raof52046b2009-01-09 01:49:01 +0100293 return 0;
294}
295
Axel Lin12065522011-03-23 20:54:17 +0800296static const struct i2c_device_id pcf50633_id_table[] = {
Balaji Raof52046b2009-01-09 01:49:01 +0100297 {"pcf50633", 0x73},
Jean Delvare8915e542009-02-17 09:07:02 +0100298 {/* end of list */}
Balaji Raof52046b2009-01-09 01:49:01 +0100299};
Axel Lin76790892011-02-28 14:33:12 +0800300MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
Balaji Raof52046b2009-01-09 01:49:01 +0100301
302static struct i2c_driver pcf50633_driver = {
303 .driver = {
304 .name = "pcf50633",
Mark Brown939941d2011-01-20 21:52:42 +0000305 .pm = &pcf50633_pm,
Balaji Raof52046b2009-01-09 01:49:01 +0100306 },
307 .id_table = pcf50633_id_table,
308 .probe = pcf50633_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500309 .remove = pcf50633_remove,
Balaji Raof52046b2009-01-09 01:49:01 +0100310};
311
312static int __init pcf50633_init(void)
313{
314 return i2c_add_driver(&pcf50633_driver);
315}
316
317static void __exit pcf50633_exit(void)
318{
319 i2c_del_driver(&pcf50633_driver);
320}
321
322MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
323MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
324MODULE_LICENSE("GPL");
325
Samuel Ortiz2021de82009-06-15 18:04:54 +0200326subsys_initcall(pcf50633_init);
Balaji Raof52046b2009-01-09 01:49:01 +0100327module_exit(pcf50633_exit);