xref: /openbmc/linux/drivers/mfd/pcf50633-core.c (revision 970d9fbca95c2f5277a4f55c2fba9a8b615c38f7)
1f52046b1SBalaji Rao /* NXP PCF50633 Power Management Unit (PMU) driver
2f52046b1SBalaji Rao  *
3f52046b1SBalaji Rao  * (C) 2006-2008 by Openmoko, Inc.
4f52046b1SBalaji Rao  * Author: Harald Welte <laforge@openmoko.org>
5f52046b1SBalaji Rao  * 	   Balaji Rao <balajirrao@openmoko.org>
6f52046b1SBalaji Rao  * All rights reserved.
7f52046b1SBalaji Rao  *
8f52046b1SBalaji Rao  *  This program is free software; you can redistribute  it and/or modify it
9f52046b1SBalaji Rao  *  under  the terms of  the GNU General  Public License as published by the
10f52046b1SBalaji Rao  *  Free Software Foundation;  either version 2 of the  License, or (at your
11f52046b1SBalaji Rao  *  option) any later version.
12f52046b1SBalaji Rao  *
13f52046b1SBalaji Rao  */
14f52046b1SBalaji Rao 
15f52046b1SBalaji Rao #include <linux/kernel.h>
16f52046b1SBalaji Rao #include <linux/device.h>
17f52046b1SBalaji Rao #include <linux/sysfs.h>
18f52046b1SBalaji Rao #include <linux/module.h>
19f52046b1SBalaji Rao #include <linux/types.h>
20f52046b1SBalaji Rao #include <linux/interrupt.h>
21f52046b1SBalaji Rao #include <linux/workqueue.h>
22f52046b1SBalaji Rao #include <linux/platform_device.h>
23f52046b1SBalaji Rao #include <linux/i2c.h>
24939941d4SMark Brown #include <linux/pm.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
266e3ad118SMark Brown #include <linux/regmap.h>
276e3ad118SMark Brown #include <linux/err.h>
28f52046b1SBalaji Rao 
29f52046b1SBalaji Rao #include <linux/mfd/pcf50633/core.h>
30f52046b1SBalaji Rao 
31f52046b1SBalaji Rao /* Read a block of up to 32 regs  */
32f52046b1SBalaji Rao int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
33f52046b1SBalaji Rao 					int nr_regs, u8 *data)
34f52046b1SBalaji Rao {
35f52046b1SBalaji Rao 	int ret;
36f52046b1SBalaji Rao 
376e3ad118SMark Brown 	ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
386e3ad118SMark Brown 	if (ret != 0)
39f52046b1SBalaji Rao 		return ret;
406e3ad118SMark Brown 
416e3ad118SMark Brown 	return nr_regs;
42f52046b1SBalaji Rao }
43f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_read_block);
44f52046b1SBalaji Rao 
45f52046b1SBalaji Rao /* Write a block of up to 32 regs  */
46f52046b1SBalaji Rao int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
47f52046b1SBalaji Rao 					int nr_regs, u8 *data)
48f52046b1SBalaji Rao {
4960b5c5a4SAxel Lin 	return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
50f52046b1SBalaji Rao }
51f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_write_block);
52f52046b1SBalaji Rao 
53f52046b1SBalaji Rao u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
54f52046b1SBalaji Rao {
556e3ad118SMark Brown 	unsigned int val;
566e3ad118SMark Brown 	int ret;
57f52046b1SBalaji Rao 
586e3ad118SMark Brown 	ret = regmap_read(pcf->regmap, reg, &val);
596e3ad118SMark Brown 	if (ret < 0)
606e3ad118SMark Brown 		return -1;
61f52046b1SBalaji Rao 
62f52046b1SBalaji Rao 	return val;
63f52046b1SBalaji Rao }
64f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_read);
65f52046b1SBalaji Rao 
66f52046b1SBalaji Rao int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
67f52046b1SBalaji Rao {
686e3ad118SMark Brown 	return regmap_write(pcf->regmap, reg, val);
69f52046b1SBalaji Rao }
70f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_write);
71f52046b1SBalaji Rao 
72f52046b1SBalaji Rao int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
73f52046b1SBalaji Rao {
746e3ad118SMark Brown 	return regmap_update_bits(pcf->regmap, reg, mask, val);
75f52046b1SBalaji Rao }
76f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
77f52046b1SBalaji Rao 
78f52046b1SBalaji Rao int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
79f52046b1SBalaji Rao {
806e3ad118SMark Brown 	return regmap_update_bits(pcf->regmap, reg, val, 0);
81f52046b1SBalaji Rao }
82f52046b1SBalaji Rao EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
83f52046b1SBalaji Rao 
84f52046b1SBalaji Rao /* sysfs attributes */
85f52046b1SBalaji Rao static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
86f52046b1SBalaji Rao 			    char *buf)
87f52046b1SBalaji Rao {
88f52046b1SBalaji Rao 	struct pcf50633 *pcf = dev_get_drvdata(dev);
89f52046b1SBalaji Rao 	u8 dump[16];
90f52046b1SBalaji Rao 	int n, n1, idx = 0;
91f52046b1SBalaji Rao 	char *buf1 = buf;
92f52046b1SBalaji Rao 	static u8 address_no_read[] = { /* must be ascending */
93f52046b1SBalaji Rao 		PCF50633_REG_INT1,
94f52046b1SBalaji Rao 		PCF50633_REG_INT2,
95f52046b1SBalaji Rao 		PCF50633_REG_INT3,
96f52046b1SBalaji Rao 		PCF50633_REG_INT4,
97f52046b1SBalaji Rao 		PCF50633_REG_INT5,
98f52046b1SBalaji Rao 		0 /* terminator */
99f52046b1SBalaji Rao 	};
100f52046b1SBalaji Rao 
101f52046b1SBalaji Rao 	for (n = 0; n < 256; n += sizeof(dump)) {
102f52046b1SBalaji Rao 		for (n1 = 0; n1 < sizeof(dump); n1++)
103f52046b1SBalaji Rao 			if (n == address_no_read[idx]) {
104f52046b1SBalaji Rao 				idx++;
105f52046b1SBalaji Rao 				dump[n1] = 0x00;
106f52046b1SBalaji Rao 			} else
107f52046b1SBalaji Rao 				dump[n1] = pcf50633_reg_read(pcf, n + n1);
108f52046b1SBalaji Rao 
109*970d9fbcSAndy Shevchenko 		buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
110f52046b1SBalaji Rao 	}
111f52046b1SBalaji Rao 
112f52046b1SBalaji Rao 	return buf1 - buf;
113f52046b1SBalaji Rao }
114f52046b1SBalaji Rao static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
115f52046b1SBalaji Rao 
116f52046b1SBalaji Rao static ssize_t show_resume_reason(struct device *dev,
117f52046b1SBalaji Rao 				struct device_attribute *attr, char *buf)
118f52046b1SBalaji Rao {
119f52046b1SBalaji Rao 	struct pcf50633 *pcf = dev_get_drvdata(dev);
120f52046b1SBalaji Rao 	int n;
121f52046b1SBalaji Rao 
122f52046b1SBalaji Rao 	n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
123f52046b1SBalaji Rao 				pcf->resume_reason[0],
124f52046b1SBalaji Rao 				pcf->resume_reason[1],
125f52046b1SBalaji Rao 				pcf->resume_reason[2],
126f52046b1SBalaji Rao 				pcf->resume_reason[3],
127f52046b1SBalaji Rao 				pcf->resume_reason[4]);
128f52046b1SBalaji Rao 
129f52046b1SBalaji Rao 	return n;
130f52046b1SBalaji Rao }
131f52046b1SBalaji Rao static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
132f52046b1SBalaji Rao 
133f52046b1SBalaji Rao static struct attribute *pcf_sysfs_entries[] = {
134f52046b1SBalaji Rao 	&dev_attr_dump_regs.attr,
135f52046b1SBalaji Rao 	&dev_attr_resume_reason.attr,
136f52046b1SBalaji Rao 	NULL,
137f52046b1SBalaji Rao };
138f52046b1SBalaji Rao 
139f52046b1SBalaji Rao static struct attribute_group pcf_attr_group = {
140f52046b1SBalaji Rao 	.name	= NULL,			/* put in device directory */
141f52046b1SBalaji Rao 	.attrs	= pcf_sysfs_entries,
142f52046b1SBalaji Rao };
143f52046b1SBalaji Rao 
144f52046b1SBalaji Rao static void
145f52046b1SBalaji Rao pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
146f52046b1SBalaji Rao 						struct platform_device **pdev)
147f52046b1SBalaji Rao {
148f52046b1SBalaji Rao 	int ret;
149f52046b1SBalaji Rao 
150f52046b1SBalaji Rao 	*pdev = platform_device_alloc(name, -1);
151f52046b1SBalaji Rao 	if (!*pdev) {
152f52046b1SBalaji Rao 		dev_err(pcf->dev, "Falied to allocate %s\n", name);
153f52046b1SBalaji Rao 		return;
154f52046b1SBalaji Rao 	}
155f52046b1SBalaji Rao 
156f52046b1SBalaji Rao 	(*pdev)->dev.parent = pcf->dev;
157f52046b1SBalaji Rao 
158f52046b1SBalaji Rao 	ret = platform_device_add(*pdev);
159f52046b1SBalaji Rao 	if (ret) {
160f52046b1SBalaji Rao 		dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
161f52046b1SBalaji Rao 		platform_device_put(*pdev);
162f52046b1SBalaji Rao 		*pdev = NULL;
163f52046b1SBalaji Rao 	}
164f52046b1SBalaji Rao }
165f52046b1SBalaji Rao 
166939941d4SMark Brown #ifdef CONFIG_PM_SLEEP
167939941d4SMark Brown static int pcf50633_suspend(struct device *dev)
168f52046b1SBalaji Rao {
169939941d4SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
170939941d4SMark Brown 	struct pcf50633 *pcf = i2c_get_clientdata(client);
171f52046b1SBalaji Rao 
172380c09f6SLars-Peter Clausen 	return pcf50633_irq_suspend(pcf);
173f52046b1SBalaji Rao }
174f52046b1SBalaji Rao 
175939941d4SMark Brown static int pcf50633_resume(struct device *dev)
176f52046b1SBalaji Rao {
177939941d4SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
178939941d4SMark Brown 	struct pcf50633 *pcf = i2c_get_clientdata(client);
179f52046b1SBalaji Rao 
180380c09f6SLars-Peter Clausen 	return pcf50633_irq_resume(pcf);
181f52046b1SBalaji Rao }
182f52046b1SBalaji Rao #endif
183f52046b1SBalaji Rao 
184939941d4SMark Brown static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
185939941d4SMark Brown 
1866e3ad118SMark Brown static struct regmap_config pcf50633_regmap_config = {
1876e3ad118SMark Brown 	.reg_bits = 8,
1886e3ad118SMark Brown 	.val_bits = 8,
1896e3ad118SMark Brown };
1906e3ad118SMark Brown 
191f791be49SBill Pemberton static int pcf50633_probe(struct i2c_client *client,
192f52046b1SBalaji Rao 				const struct i2c_device_id *ids)
193f52046b1SBalaji Rao {
194f52046b1SBalaji Rao 	struct pcf50633 *pcf;
195cddc1141SLee Jones 	struct platform_device *pdev;
196334a41ceSJingoo Han 	struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
197cddc1141SLee Jones 	int i, j, ret;
198f52046b1SBalaji Rao 	int version, variant;
199f52046b1SBalaji Rao 
20024213ae1SLars-Peter Clausen 	if (!client->irq) {
20124213ae1SLars-Peter Clausen 		dev_err(&client->dev, "Missing IRQ\n");
20224213ae1SLars-Peter Clausen 		return -ENOENT;
20324213ae1SLars-Peter Clausen 	}
20424213ae1SLars-Peter Clausen 
205aa4603a0SAxel Lin 	pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
206f52046b1SBalaji Rao 	if (!pcf)
207f52046b1SBalaji Rao 		return -ENOMEM;
208f52046b1SBalaji Rao 
209b30dd8f2SAxel Lin 	i2c_set_clientdata(client, pcf);
210b30dd8f2SAxel Lin 	pcf->dev = &client->dev;
211f52046b1SBalaji Rao 	pcf->pdata = pdata;
212f52046b1SBalaji Rao 
213f52046b1SBalaji Rao 	mutex_init(&pcf->lock);
214f52046b1SBalaji Rao 
215aa4603a0SAxel Lin 	pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
2166e3ad118SMark Brown 	if (IS_ERR(pcf->regmap)) {
2176e3ad118SMark Brown 		ret = PTR_ERR(pcf->regmap);
218aa4603a0SAxel Lin 		dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
219aa4603a0SAxel Lin 		return ret;
2206e3ad118SMark Brown 	}
2216e3ad118SMark Brown 
222f52046b1SBalaji Rao 	version = pcf50633_reg_read(pcf, 0);
223f52046b1SBalaji Rao 	variant = pcf50633_reg_read(pcf, 1);
224f52046b1SBalaji Rao 	if (version < 0 || variant < 0) {
225f52046b1SBalaji Rao 		dev_err(pcf->dev, "Unable to probe pcf50633\n");
226f52046b1SBalaji Rao 		ret = -ENODEV;
227aa4603a0SAxel Lin 		return ret;
228f52046b1SBalaji Rao 	}
229f52046b1SBalaji Rao 
230f52046b1SBalaji Rao 	dev_info(pcf->dev, "Probed device version %d variant %d\n",
231f52046b1SBalaji Rao 							version, variant);
232f52046b1SBalaji Rao 
233380c09f6SLars-Peter Clausen 	pcf50633_irq_init(pcf, client->irq);
23424213ae1SLars-Peter Clausen 
235f52046b1SBalaji Rao 	/* Create sub devices */
236aa4603a0SAxel Lin 	pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
237aa4603a0SAxel Lin 	pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
238aa4603a0SAxel Lin 	pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
239aa4603a0SAxel Lin 	pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
240aa4603a0SAxel Lin 	pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
241f5bf403aSLars-Peter Clausen 
242f52046b1SBalaji Rao 
243f52046b1SBalaji Rao 	for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
244561427f5SAxel Lin 		pdev = platform_device_alloc("pcf50633-regulator", i);
245c981015eSLee Jones 		if (!pdev)
246c981015eSLee Jones 			return -ENOMEM;
247f52046b1SBalaji Rao 
248f52046b1SBalaji Rao 		pdev->dev.parent = pcf->dev;
249c981015eSLee Jones 		ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
250c981015eSLee Jones 					       sizeof(pdata->reg_init_data[i]));
251cddc1141SLee Jones 		if (ret)
252cddc1141SLee Jones 			goto err;
253f52046b1SBalaji Rao 
254cddc1141SLee Jones 		ret = platform_device_add(pdev);
255cddc1141SLee Jones 		if (ret)
256cddc1141SLee Jones 			goto err;
257cddc1141SLee Jones 
258cddc1141SLee Jones 		pcf->regulator_pdev[i] = pdev;
259f52046b1SBalaji Rao 	}
260f52046b1SBalaji Rao 
261f52046b1SBalaji Rao 	ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
262f52046b1SBalaji Rao 	if (ret)
263cddc1141SLee Jones 		dev_warn(pcf->dev, "error creating sysfs entries\n");
264f52046b1SBalaji Rao 
265f52046b1SBalaji Rao 	if (pdata->probe_done)
266f52046b1SBalaji Rao 		pdata->probe_done(pcf);
267f52046b1SBalaji Rao 
268f52046b1SBalaji Rao 	return 0;
269cddc1141SLee Jones 
270cddc1141SLee Jones err:
271cddc1141SLee Jones 	platform_device_put(pdev);
272cddc1141SLee Jones 	for (j = 0; j < i; j++)
273cddc1141SLee Jones 		platform_device_put(pcf->regulator_pdev[j]);
274cddc1141SLee Jones 
275cddc1141SLee Jones 	return ret;
276f52046b1SBalaji Rao }
277f52046b1SBalaji Rao 
2784740f73fSBill Pemberton static int pcf50633_remove(struct i2c_client *client)
279f52046b1SBalaji Rao {
280f52046b1SBalaji Rao 	struct pcf50633 *pcf = i2c_get_clientdata(client);
281f52046b1SBalaji Rao 	int i;
282f52046b1SBalaji Rao 
2838220fe4cSAxel Lin 	sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
284380c09f6SLars-Peter Clausen 	pcf50633_irq_free(pcf);
285f52046b1SBalaji Rao 
286f52046b1SBalaji Rao 	platform_device_unregister(pcf->input_pdev);
287f52046b1SBalaji Rao 	platform_device_unregister(pcf->rtc_pdev);
288f52046b1SBalaji Rao 	platform_device_unregister(pcf->mbc_pdev);
289f52046b1SBalaji Rao 	platform_device_unregister(pcf->adc_pdev);
2908220fe4cSAxel Lin 	platform_device_unregister(pcf->bl_pdev);
291f52046b1SBalaji Rao 
292f52046b1SBalaji Rao 	for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
293f52046b1SBalaji Rao 		platform_device_unregister(pcf->regulator_pdev[i]);
294f52046b1SBalaji Rao 
295f52046b1SBalaji Rao 	return 0;
296f52046b1SBalaji Rao }
297f52046b1SBalaji Rao 
2981206552bSAxel Lin static const struct i2c_device_id pcf50633_id_table[] = {
299f52046b1SBalaji Rao 	{"pcf50633", 0x73},
3008915e540SJean Delvare 	{/* end of list */}
301f52046b1SBalaji Rao };
3027679089dSAxel Lin MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
303f52046b1SBalaji Rao 
304f52046b1SBalaji Rao static struct i2c_driver pcf50633_driver = {
305f52046b1SBalaji Rao 	.driver = {
306f52046b1SBalaji Rao 		.name	= "pcf50633",
307939941d4SMark Brown 		.pm	= &pcf50633_pm,
308f52046b1SBalaji Rao 	},
309f52046b1SBalaji Rao 	.id_table = pcf50633_id_table,
310f52046b1SBalaji Rao 	.probe = pcf50633_probe,
31184449216SBill Pemberton 	.remove = pcf50633_remove,
312f52046b1SBalaji Rao };
313f52046b1SBalaji Rao 
314f52046b1SBalaji Rao static int __init pcf50633_init(void)
315f52046b1SBalaji Rao {
316f52046b1SBalaji Rao 	return i2c_add_driver(&pcf50633_driver);
317f52046b1SBalaji Rao }
318f52046b1SBalaji Rao 
319f52046b1SBalaji Rao static void __exit pcf50633_exit(void)
320f52046b1SBalaji Rao {
321f52046b1SBalaji Rao 	i2c_del_driver(&pcf50633_driver);
322f52046b1SBalaji Rao }
323f52046b1SBalaji Rao 
324f52046b1SBalaji Rao MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
325f52046b1SBalaji Rao MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
326f52046b1SBalaji Rao MODULE_LICENSE("GPL");
327f52046b1SBalaji Rao 
3282021de87SSamuel Ortiz subsys_initcall(pcf50633_init);
329f52046b1SBalaji Rao module_exit(pcf50633_exit);
330