12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f52046b1SBalaji Rao /* NXP PCF50633 Power Management Unit (PMU) driver
3f52046b1SBalaji Rao *
4f52046b1SBalaji Rao * (C) 2006-2008 by Openmoko, Inc.
5f52046b1SBalaji Rao * Author: Harald Welte <laforge@openmoko.org>
6f52046b1SBalaji Rao * Balaji Rao <balajirrao@openmoko.org>
7f52046b1SBalaji Rao * All rights reserved.
8f52046b1SBalaji Rao */
9f52046b1SBalaji Rao
10f52046b1SBalaji Rao #include <linux/kernel.h>
11f52046b1SBalaji Rao #include <linux/device.h>
12f52046b1SBalaji Rao #include <linux/sysfs.h>
13f52046b1SBalaji Rao #include <linux/module.h>
14f52046b1SBalaji Rao #include <linux/types.h>
15f52046b1SBalaji Rao #include <linux/interrupt.h>
16f52046b1SBalaji Rao #include <linux/workqueue.h>
17f52046b1SBalaji Rao #include <linux/platform_device.h>
18f52046b1SBalaji Rao #include <linux/i2c.h>
19939941d4SMark Brown #include <linux/pm.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
216e3ad118SMark Brown #include <linux/regmap.h>
226e3ad118SMark Brown #include <linux/err.h>
23f52046b1SBalaji Rao
24f52046b1SBalaji Rao #include <linux/mfd/pcf50633/core.h>
25f52046b1SBalaji Rao
26f52046b1SBalaji Rao /* Read a block of up to 32 regs */
pcf50633_read_block(struct pcf50633 * pcf,u8 reg,int nr_regs,u8 * data)27f52046b1SBalaji Rao int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
28f52046b1SBalaji Rao int nr_regs, u8 *data)
29f52046b1SBalaji Rao {
30f52046b1SBalaji Rao int ret;
31f52046b1SBalaji Rao
326e3ad118SMark Brown ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
336e3ad118SMark Brown if (ret != 0)
34f52046b1SBalaji Rao return ret;
356e3ad118SMark Brown
366e3ad118SMark Brown return nr_regs;
37f52046b1SBalaji Rao }
38f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_read_block);
39f52046b1SBalaji Rao
40f52046b1SBalaji Rao /* Write a block of up to 32 regs */
pcf50633_write_block(struct pcf50633 * pcf,u8 reg,int nr_regs,u8 * data)41f52046b1SBalaji Rao int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
42f52046b1SBalaji Rao int nr_regs, u8 *data)
43f52046b1SBalaji Rao {
4460b5c5a4SAxel Lin return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
45f52046b1SBalaji Rao }
46f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_write_block);
47f52046b1SBalaji Rao
pcf50633_reg_read(struct pcf50633 * pcf,u8 reg)48f52046b1SBalaji Rao u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
49f52046b1SBalaji Rao {
506e3ad118SMark Brown unsigned int val;
516e3ad118SMark Brown int ret;
52f52046b1SBalaji Rao
536e3ad118SMark Brown ret = regmap_read(pcf->regmap, reg, &val);
546e3ad118SMark Brown if (ret < 0)
556e3ad118SMark Brown return -1;
56f52046b1SBalaji Rao
57f52046b1SBalaji Rao return val;
58f52046b1SBalaji Rao }
59f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_read);
60f52046b1SBalaji Rao
pcf50633_reg_write(struct pcf50633 * pcf,u8 reg,u8 val)61f52046b1SBalaji Rao int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
62f52046b1SBalaji Rao {
636e3ad118SMark Brown return regmap_write(pcf->regmap, reg, val);
64f52046b1SBalaji Rao }
65f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_write);
66f52046b1SBalaji Rao
pcf50633_reg_set_bit_mask(struct pcf50633 * pcf,u8 reg,u8 mask,u8 val)67f52046b1SBalaji Rao int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
68f52046b1SBalaji Rao {
696e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, mask, val);
70f52046b1SBalaji Rao }
71f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
72f52046b1SBalaji Rao
pcf50633_reg_clear_bits(struct pcf50633 * pcf,u8 reg,u8 val)73f52046b1SBalaji Rao int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
74f52046b1SBalaji Rao {
756e3ad118SMark Brown return regmap_update_bits(pcf->regmap, reg, val, 0);
76f52046b1SBalaji Rao }
77f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
78f52046b1SBalaji Rao
79f52046b1SBalaji Rao /* sysfs attributes */
dump_regs_show(struct device * dev,struct device_attribute * attr,char * buf)80426d94a9SZhen Lei static ssize_t dump_regs_show(struct device *dev,
81426d94a9SZhen Lei struct device_attribute *attr, char *buf)
82f52046b1SBalaji Rao {
83f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev);
84f52046b1SBalaji Rao u8 dump[16];
85f52046b1SBalaji Rao int n, n1, idx = 0;
86f52046b1SBalaji Rao char *buf1 = buf;
87f52046b1SBalaji Rao static u8 address_no_read[] = { /* must be ascending */
88f52046b1SBalaji Rao PCF50633_REG_INT1,
89f52046b1SBalaji Rao PCF50633_REG_INT2,
90f52046b1SBalaji Rao PCF50633_REG_INT3,
91f52046b1SBalaji Rao PCF50633_REG_INT4,
92f52046b1SBalaji Rao PCF50633_REG_INT5,
93f52046b1SBalaji Rao 0 /* terminator */
94f52046b1SBalaji Rao };
95f52046b1SBalaji Rao
96f52046b1SBalaji Rao for (n = 0; n < 256; n += sizeof(dump)) {
97f52046b1SBalaji Rao for (n1 = 0; n1 < sizeof(dump); n1++)
98f52046b1SBalaji Rao if (n == address_no_read[idx]) {
99f52046b1SBalaji Rao idx++;
100f52046b1SBalaji Rao dump[n1] = 0x00;
101f52046b1SBalaji Rao } else
102f52046b1SBalaji Rao dump[n1] = pcf50633_reg_read(pcf, n + n1);
103f52046b1SBalaji Rao
104970d9fbcSAndy Shevchenko buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
105f52046b1SBalaji Rao }
106f52046b1SBalaji Rao
107f52046b1SBalaji Rao return buf1 - buf;
108f52046b1SBalaji Rao }
109426d94a9SZhen Lei static DEVICE_ATTR_ADMIN_RO(dump_regs);
110f52046b1SBalaji Rao
resume_reason_show(struct device * dev,struct device_attribute * attr,char * buf)111426d94a9SZhen Lei static ssize_t resume_reason_show(struct device *dev,
112f52046b1SBalaji Rao struct device_attribute *attr, char *buf)
113f52046b1SBalaji Rao {
114f52046b1SBalaji Rao struct pcf50633 *pcf = dev_get_drvdata(dev);
115f52046b1SBalaji Rao int n;
116f52046b1SBalaji Rao
117f52046b1SBalaji Rao n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
118f52046b1SBalaji Rao pcf->resume_reason[0],
119f52046b1SBalaji Rao pcf->resume_reason[1],
120f52046b1SBalaji Rao pcf->resume_reason[2],
121f52046b1SBalaji Rao pcf->resume_reason[3],
122f52046b1SBalaji Rao pcf->resume_reason[4]);
123f52046b1SBalaji Rao
124f52046b1SBalaji Rao return n;
125f52046b1SBalaji Rao }
126426d94a9SZhen Lei static DEVICE_ATTR_ADMIN_RO(resume_reason);
127f52046b1SBalaji Rao
128f52046b1SBalaji Rao static struct attribute *pcf_sysfs_entries[] = {
129f52046b1SBalaji Rao &dev_attr_dump_regs.attr,
130f52046b1SBalaji Rao &dev_attr_resume_reason.attr,
131f52046b1SBalaji Rao NULL,
132f52046b1SBalaji Rao };
133f52046b1SBalaji Rao
134f52046b1SBalaji Rao static struct attribute_group pcf_attr_group = {
135f52046b1SBalaji Rao .name = NULL, /* put in device directory */
136f52046b1SBalaji Rao .attrs = pcf_sysfs_entries,
137f52046b1SBalaji Rao };
138f52046b1SBalaji Rao
139f52046b1SBalaji Rao static void
pcf50633_client_dev_register(struct pcf50633 * pcf,const char * name,struct platform_device ** pdev)140f52046b1SBalaji Rao pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
141f52046b1SBalaji Rao struct platform_device **pdev)
142f52046b1SBalaji Rao {
143f52046b1SBalaji Rao int ret;
144f52046b1SBalaji Rao
145f52046b1SBalaji Rao *pdev = platform_device_alloc(name, -1);
146f52046b1SBalaji Rao if (!*pdev) {
147f058aa3fSArvind Yadav dev_err(pcf->dev, "Failed to allocate %s\n", name);
148f52046b1SBalaji Rao return;
149f52046b1SBalaji Rao }
150f52046b1SBalaji Rao
151f52046b1SBalaji Rao (*pdev)->dev.parent = pcf->dev;
152f52046b1SBalaji Rao
153f52046b1SBalaji Rao ret = platform_device_add(*pdev);
154f52046b1SBalaji Rao if (ret) {
155f52046b1SBalaji Rao dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
156f52046b1SBalaji Rao platform_device_put(*pdev);
157f52046b1SBalaji Rao *pdev = NULL;
158f52046b1SBalaji Rao }
159f52046b1SBalaji Rao }
160f52046b1SBalaji Rao
1611590d4a1SKrzysztof Kozlowski static const struct regmap_config pcf50633_regmap_config = {
1626e3ad118SMark Brown .reg_bits = 8,
1636e3ad118SMark Brown .val_bits = 8,
1646e3ad118SMark Brown };
1656e3ad118SMark Brown
pcf50633_probe(struct i2c_client * client)166659ea732SUwe Kleine-König static int pcf50633_probe(struct i2c_client *client)
167f52046b1SBalaji Rao {
168f52046b1SBalaji Rao struct pcf50633 *pcf;
169cddc1141SLee Jones struct platform_device *pdev;
170334a41ceSJingoo Han struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
171cddc1141SLee Jones int i, j, ret;
172f52046b1SBalaji Rao int version, variant;
173f52046b1SBalaji Rao
17424213ae1SLars-Peter Clausen if (!client->irq) {
17524213ae1SLars-Peter Clausen dev_err(&client->dev, "Missing IRQ\n");
17624213ae1SLars-Peter Clausen return -ENOENT;
17724213ae1SLars-Peter Clausen }
17824213ae1SLars-Peter Clausen
179aa4603a0SAxel Lin pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
180f52046b1SBalaji Rao if (!pcf)
181f52046b1SBalaji Rao return -ENOMEM;
182f52046b1SBalaji Rao
183b30dd8f2SAxel Lin i2c_set_clientdata(client, pcf);
184b30dd8f2SAxel Lin pcf->dev = &client->dev;
185f52046b1SBalaji Rao pcf->pdata = pdata;
186f52046b1SBalaji Rao
187f52046b1SBalaji Rao mutex_init(&pcf->lock);
188f52046b1SBalaji Rao
189aa4603a0SAxel Lin pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
1906e3ad118SMark Brown if (IS_ERR(pcf->regmap)) {
1916e3ad118SMark Brown ret = PTR_ERR(pcf->regmap);
192aa4603a0SAxel Lin dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
193aa4603a0SAxel Lin return ret;
1946e3ad118SMark Brown }
1956e3ad118SMark Brown
196f52046b1SBalaji Rao version = pcf50633_reg_read(pcf, 0);
197f52046b1SBalaji Rao variant = pcf50633_reg_read(pcf, 1);
198f52046b1SBalaji Rao if (version < 0 || variant < 0) {
199f52046b1SBalaji Rao dev_err(pcf->dev, "Unable to probe pcf50633\n");
200f52046b1SBalaji Rao ret = -ENODEV;
201aa4603a0SAxel Lin return ret;
202f52046b1SBalaji Rao }
203f52046b1SBalaji Rao
204f52046b1SBalaji Rao dev_info(pcf->dev, "Probed device version %d variant %d\n",
205f52046b1SBalaji Rao version, variant);
206f52046b1SBalaji Rao
207380c09f6SLars-Peter Clausen pcf50633_irq_init(pcf, client->irq);
20824213ae1SLars-Peter Clausen
209f52046b1SBalaji Rao /* Create sub devices */
210aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
211aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
212aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
213aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
214aa4603a0SAxel Lin pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
215f5bf403aSLars-Peter Clausen
216f52046b1SBalaji Rao
217f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
218561427f5SAxel Lin pdev = platform_device_alloc("pcf50633-regulator", i);
21952a3a374SChristophe JAILLET if (!pdev) {
22052a3a374SChristophe JAILLET ret = -ENOMEM;
22152a3a374SChristophe JAILLET goto err2;
22252a3a374SChristophe JAILLET }
223f52046b1SBalaji Rao
224f52046b1SBalaji Rao pdev->dev.parent = pcf->dev;
225c981015eSLee Jones ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
226c981015eSLee Jones sizeof(pdata->reg_init_data[i]));
227cddc1141SLee Jones if (ret)
228cddc1141SLee Jones goto err;
229f52046b1SBalaji Rao
230cddc1141SLee Jones ret = platform_device_add(pdev);
231cddc1141SLee Jones if (ret)
232cddc1141SLee Jones goto err;
233cddc1141SLee Jones
234cddc1141SLee Jones pcf->regulator_pdev[i] = pdev;
235f52046b1SBalaji Rao }
236f52046b1SBalaji Rao
237f52046b1SBalaji Rao ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
238f52046b1SBalaji Rao if (ret)
239cddc1141SLee Jones dev_warn(pcf->dev, "error creating sysfs entries\n");
240f52046b1SBalaji Rao
241f52046b1SBalaji Rao if (pdata->probe_done)
242f52046b1SBalaji Rao pdata->probe_done(pcf);
243f52046b1SBalaji Rao
244f52046b1SBalaji Rao return 0;
245cddc1141SLee Jones
246cddc1141SLee Jones err:
247cddc1141SLee Jones platform_device_put(pdev);
24852a3a374SChristophe JAILLET err2:
249cddc1141SLee Jones for (j = 0; j < i; j++)
250cddc1141SLee Jones platform_device_put(pcf->regulator_pdev[j]);
251cddc1141SLee Jones
252cddc1141SLee Jones return ret;
253f52046b1SBalaji Rao }
254f52046b1SBalaji Rao
pcf50633_remove(struct i2c_client * client)255ed5c2f5fSUwe Kleine-König static void pcf50633_remove(struct i2c_client *client)
256f52046b1SBalaji Rao {
257f52046b1SBalaji Rao struct pcf50633 *pcf = i2c_get_clientdata(client);
258f52046b1SBalaji Rao int i;
259f52046b1SBalaji Rao
2608220fe4cSAxel Lin sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
261380c09f6SLars-Peter Clausen pcf50633_irq_free(pcf);
262f52046b1SBalaji Rao
263f52046b1SBalaji Rao platform_device_unregister(pcf->input_pdev);
264f52046b1SBalaji Rao platform_device_unregister(pcf->rtc_pdev);
265f52046b1SBalaji Rao platform_device_unregister(pcf->mbc_pdev);
266f52046b1SBalaji Rao platform_device_unregister(pcf->adc_pdev);
2678220fe4cSAxel Lin platform_device_unregister(pcf->bl_pdev);
268f52046b1SBalaji Rao
269f52046b1SBalaji Rao for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
270f52046b1SBalaji Rao platform_device_unregister(pcf->regulator_pdev[i]);
271f52046b1SBalaji Rao }
272f52046b1SBalaji Rao
2731206552bSAxel Lin static const struct i2c_device_id pcf50633_id_table[] = {
274f52046b1SBalaji Rao {"pcf50633", 0x73},
2758915e540SJean Delvare {/* end of list */}
276f52046b1SBalaji Rao };
2777679089dSAxel Lin MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
278f52046b1SBalaji Rao
279f52046b1SBalaji Rao static struct i2c_driver pcf50633_driver = {
280f52046b1SBalaji Rao .driver = {
281f52046b1SBalaji Rao .name = "pcf50633",
282245cb473SPaul Cercueil .pm = pm_sleep_ptr(&pcf50633_pm),
283f52046b1SBalaji Rao },
284f52046b1SBalaji Rao .id_table = pcf50633_id_table,
285*9816d859SUwe Kleine-König .probe = pcf50633_probe,
28684449216SBill Pemberton .remove = pcf50633_remove,
287f52046b1SBalaji Rao };
288f52046b1SBalaji Rao
pcf50633_init(void)289f52046b1SBalaji Rao static int __init pcf50633_init(void)
290f52046b1SBalaji Rao {
291f52046b1SBalaji Rao return i2c_add_driver(&pcf50633_driver);
292f52046b1SBalaji Rao }
293f52046b1SBalaji Rao
pcf50633_exit(void)294f52046b1SBalaji Rao static void __exit pcf50633_exit(void)
295f52046b1SBalaji Rao {
296f52046b1SBalaji Rao i2c_del_driver(&pcf50633_driver);
297f52046b1SBalaji Rao }
298f52046b1SBalaji Rao
299f52046b1SBalaji Rao MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
300f52046b1SBalaji Rao MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
301f52046b1SBalaji Rao MODULE_LICENSE("GPL");
302f52046b1SBalaji Rao
3032021de87SSamuel Ortiz subsys_initcall(pcf50633_init);
304f52046b1SBalaji Rao module_exit(pcf50633_exit);
305