xref: /openbmc/linux/drivers/mfd/pcf50633-core.c (revision 2455f0e1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* 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.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sysfs.h>
13 #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>
19 #include <linux/pm.h>
20 #include <linux/slab.h>
21 #include <linux/regmap.h>
22 #include <linux/err.h>
23 
24 #include <linux/mfd/pcf50633/core.h>
25 
26 /* Read a block of up to 32 regs  */
27 int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
28 					int nr_regs, u8 *data)
29 {
30 	int ret;
31 
32 	ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
33 	if (ret != 0)
34 		return ret;
35 
36 	return nr_regs;
37 }
38 EXPORT_SYMBOL_GPL(pcf50633_read_block);
39 
40 /* Write a block of up to 32 regs  */
41 int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
42 					int nr_regs, u8 *data)
43 {
44 	return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
45 }
46 EXPORT_SYMBOL_GPL(pcf50633_write_block);
47 
48 u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
49 {
50 	unsigned int val;
51 	int ret;
52 
53 	ret = regmap_read(pcf->regmap, reg, &val);
54 	if (ret < 0)
55 		return -1;
56 
57 	return val;
58 }
59 EXPORT_SYMBOL_GPL(pcf50633_reg_read);
60 
61 int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
62 {
63 	return regmap_write(pcf->regmap, reg, val);
64 }
65 EXPORT_SYMBOL_GPL(pcf50633_reg_write);
66 
67 int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
68 {
69 	return regmap_update_bits(pcf->regmap, reg, mask, val);
70 }
71 EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
72 
73 int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
74 {
75 	return regmap_update_bits(pcf->regmap, reg, val, 0);
76 }
77 EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
78 
79 /* sysfs attributes */
80 static ssize_t dump_regs_show(struct device *dev,
81 			      struct device_attribute *attr, 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 
104 		buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
105 	}
106 
107 	return buf1 - buf;
108 }
109 static DEVICE_ATTR_ADMIN_RO(dump_regs);
110 
111 static ssize_t resume_reason_show(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 }
126 static DEVICE_ATTR_ADMIN_RO(resume_reason);
127 
128 static struct attribute *pcf_sysfs_entries[] = {
129 	&dev_attr_dump_regs.attr,
130 	&dev_attr_resume_reason.attr,
131 	NULL,
132 };
133 
134 static struct attribute_group pcf_attr_group = {
135 	.name	= NULL,			/* put in device directory */
136 	.attrs	= pcf_sysfs_entries,
137 };
138 
139 static void
140 pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
141 						struct platform_device **pdev)
142 {
143 	int ret;
144 
145 	*pdev = platform_device_alloc(name, -1);
146 	if (!*pdev) {
147 		dev_err(pcf->dev, "Failed to allocate %s\n", name);
148 		return;
149 	}
150 
151 	(*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 
161 static const struct regmap_config pcf50633_regmap_config = {
162 	.reg_bits = 8,
163 	.val_bits = 8,
164 };
165 
166 static int pcf50633_probe(struct i2c_client *client)
167 {
168 	struct pcf50633 *pcf;
169 	struct platform_device *pdev;
170 	struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
171 	int i, j, ret;
172 	int version, variant;
173 
174 	if (!client->irq) {
175 		dev_err(&client->dev, "Missing IRQ\n");
176 		return -ENOENT;
177 	}
178 
179 	pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
180 	if (!pcf)
181 		return -ENOMEM;
182 
183 	i2c_set_clientdata(client, pcf);
184 	pcf->dev = &client->dev;
185 	pcf->pdata = pdata;
186 
187 	mutex_init(&pcf->lock);
188 
189 	pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
190 	if (IS_ERR(pcf->regmap)) {
191 		ret = PTR_ERR(pcf->regmap);
192 		dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
193 		return ret;
194 	}
195 
196 	version = pcf50633_reg_read(pcf, 0);
197 	variant = pcf50633_reg_read(pcf, 1);
198 	if (version < 0 || variant < 0) {
199 		dev_err(pcf->dev, "Unable to probe pcf50633\n");
200 		ret = -ENODEV;
201 		return ret;
202 	}
203 
204 	dev_info(pcf->dev, "Probed device version %d variant %d\n",
205 							version, variant);
206 
207 	pcf50633_irq_init(pcf, client->irq);
208 
209 	/* Create sub devices */
210 	pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
211 	pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
212 	pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
213 	pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
214 	pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
215 
216 
217 	for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
218 		pdev = platform_device_alloc("pcf50633-regulator", i);
219 		if (!pdev) {
220 			ret = -ENOMEM;
221 			goto err2;
222 		}
223 
224 		pdev->dev.parent = pcf->dev;
225 		ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
226 					       sizeof(pdata->reg_init_data[i]));
227 		if (ret)
228 			goto err;
229 
230 		ret = platform_device_add(pdev);
231 		if (ret)
232 			goto err;
233 
234 		pcf->regulator_pdev[i] = pdev;
235 	}
236 
237 	ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
238 	if (ret)
239 		dev_warn(pcf->dev, "error creating sysfs entries\n");
240 
241 	if (pdata->probe_done)
242 		pdata->probe_done(pcf);
243 
244 	return 0;
245 
246 err:
247 	platform_device_put(pdev);
248 err2:
249 	for (j = 0; j < i; j++)
250 		platform_device_put(pcf->regulator_pdev[j]);
251 
252 	return ret;
253 }
254 
255 static void pcf50633_remove(struct i2c_client *client)
256 {
257 	struct pcf50633 *pcf = i2c_get_clientdata(client);
258 	int i;
259 
260 	sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
261 	pcf50633_irq_free(pcf);
262 
263 	platform_device_unregister(pcf->input_pdev);
264 	platform_device_unregister(pcf->rtc_pdev);
265 	platform_device_unregister(pcf->mbc_pdev);
266 	platform_device_unregister(pcf->adc_pdev);
267 	platform_device_unregister(pcf->bl_pdev);
268 
269 	for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
270 		platform_device_unregister(pcf->regulator_pdev[i]);
271 }
272 
273 static const struct i2c_device_id pcf50633_id_table[] = {
274 	{"pcf50633", 0x73},
275 	{/* end of list */}
276 };
277 MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
278 
279 static struct i2c_driver pcf50633_driver = {
280 	.driver = {
281 		.name	= "pcf50633",
282 		.pm	= pm_sleep_ptr(&pcf50633_pm),
283 	},
284 	.id_table = pcf50633_id_table,
285 	.probe_new = pcf50633_probe,
286 	.remove = pcf50633_remove,
287 };
288 
289 static int __init pcf50633_init(void)
290 {
291 	return i2c_add_driver(&pcf50633_driver);
292 }
293 
294 static void __exit pcf50633_exit(void)
295 {
296 	i2c_del_driver(&pcf50633_driver);
297 }
298 
299 MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
300 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
301 MODULE_LICENSE("GPL");
302 
303 subsys_initcall(pcf50633_init);
304 module_exit(pcf50633_exit);
305