dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / mfd / pcf50633-core.c
blob6155d123a84e762c3491674c12200e165c21ec94
1 /* NXP PCF50633 Power Management Unit (PMU) driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
5 * Balaji Rao <balajirrao@openmoko.org>
6 * All rights reserved.
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.
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/sysfs.h>
18 #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>
24 #include <linux/pm.h>
25 #include <linux/slab.h>
26 #include <linux/regmap.h>
27 #include <linux/err.h>
29 #include <linux/mfd/pcf50633/core.h>
31 /* Read a block of up to 32 regs */
32 int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
33 int nr_regs, u8 *data)
35 int ret;
37 ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs);
38 if (ret != 0)
39 return ret;
41 return nr_regs;
43 EXPORT_SYMBOL_GPL(pcf50633_read_block);
45 /* Write a block of up to 32 regs */
46 int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
47 int nr_regs, u8 *data)
49 return regmap_raw_write(pcf->regmap, reg, data, nr_regs);
51 EXPORT_SYMBOL_GPL(pcf50633_write_block);
53 u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
55 unsigned int val;
56 int ret;
58 ret = regmap_read(pcf->regmap, reg, &val);
59 if (ret < 0)
60 return -1;
62 return val;
64 EXPORT_SYMBOL_GPL(pcf50633_reg_read);
66 int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
68 return regmap_write(pcf->regmap, reg, val);
70 EXPORT_SYMBOL_GPL(pcf50633_reg_write);
72 int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
74 return regmap_update_bits(pcf->regmap, reg, mask, val);
76 EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
78 int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
80 return regmap_update_bits(pcf->regmap, reg, val, 0);
82 EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
84 /* sysfs attributes */
85 static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
86 char *buf)
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 */
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);
109 buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
112 return buf1 - buf;
114 static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
116 static ssize_t show_resume_reason(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct pcf50633 *pcf = dev_get_drvdata(dev);
120 int n;
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]);
129 return n;
131 static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
133 static struct attribute *pcf_sysfs_entries[] = {
134 &dev_attr_dump_regs.attr,
135 &dev_attr_resume_reason.attr,
136 NULL,
139 static struct attribute_group pcf_attr_group = {
140 .name = NULL, /* put in device directory */
141 .attrs = pcf_sysfs_entries,
144 static void
145 pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
146 struct platform_device **pdev)
148 int ret;
150 *pdev = platform_device_alloc(name, -1);
151 if (!*pdev) {
152 dev_err(pcf->dev, "Falied to allocate %s\n", name);
153 return;
156 (*pdev)->dev.parent = pcf->dev;
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;
166 #ifdef CONFIG_PM_SLEEP
167 static int pcf50633_suspend(struct device *dev)
169 struct i2c_client *client = to_i2c_client(dev);
170 struct pcf50633 *pcf = i2c_get_clientdata(client);
172 return pcf50633_irq_suspend(pcf);
175 static int pcf50633_resume(struct device *dev)
177 struct i2c_client *client = to_i2c_client(dev);
178 struct pcf50633 *pcf = i2c_get_clientdata(client);
180 return pcf50633_irq_resume(pcf);
182 #endif
184 static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
186 static const struct regmap_config pcf50633_regmap_config = {
187 .reg_bits = 8,
188 .val_bits = 8,
191 static int pcf50633_probe(struct i2c_client *client,
192 const struct i2c_device_id *ids)
194 struct pcf50633 *pcf;
195 struct platform_device *pdev;
196 struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
197 int i, j, ret;
198 int version, variant;
200 if (!client->irq) {
201 dev_err(&client->dev, "Missing IRQ\n");
202 return -ENOENT;
205 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
206 if (!pcf)
207 return -ENOMEM;
209 i2c_set_clientdata(client, pcf);
210 pcf->dev = &client->dev;
211 pcf->pdata = pdata;
213 mutex_init(&pcf->lock);
215 pcf->regmap = devm_regmap_init_i2c(client, &pcf50633_regmap_config);
216 if (IS_ERR(pcf->regmap)) {
217 ret = PTR_ERR(pcf->regmap);
218 dev_err(pcf->dev, "Failed to allocate register map: %d\n", ret);
219 return ret;
222 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;
227 return ret;
230 dev_info(pcf->dev, "Probed device version %d variant %d\n",
231 version, variant);
233 pcf50633_irq_init(pcf, client->irq);
235 /* Create sub devices */
236 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);
243 for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
244 pdev = platform_device_alloc("pcf50633-regulator", i);
245 if (!pdev)
246 return -ENOMEM;
248 pdev->dev.parent = pcf->dev;
249 ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
250 sizeof(pdata->reg_init_data[i]));
251 if (ret)
252 goto err;
254 ret = platform_device_add(pdev);
255 if (ret)
256 goto err;
258 pcf->regulator_pdev[i] = pdev;
261 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
262 if (ret)
263 dev_warn(pcf->dev, "error creating sysfs entries\n");
265 if (pdata->probe_done)
266 pdata->probe_done(pcf);
268 return 0;
270 err:
271 platform_device_put(pdev);
272 for (j = 0; j < i; j++)
273 platform_device_put(pcf->regulator_pdev[j]);
275 return ret;
278 static int pcf50633_remove(struct i2c_client *client)
280 struct pcf50633 *pcf = i2c_get_clientdata(client);
281 int i;
283 sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
284 pcf50633_irq_free(pcf);
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);
290 platform_device_unregister(pcf->bl_pdev);
292 for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
293 platform_device_unregister(pcf->regulator_pdev[i]);
295 return 0;
298 static const struct i2c_device_id pcf50633_id_table[] = {
299 {"pcf50633", 0x73},
300 {/* end of list */}
302 MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
304 static struct i2c_driver pcf50633_driver = {
305 .driver = {
306 .name = "pcf50633",
307 .pm = &pcf50633_pm,
309 .id_table = pcf50633_id_table,
310 .probe = pcf50633_probe,
311 .remove = pcf50633_remove,
314 static int __init pcf50633_init(void)
316 return i2c_add_driver(&pcf50633_driver);
319 static void __exit pcf50633_exit(void)
321 i2c_del_driver(&pcf50633_driver);
324 MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
325 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
326 MODULE_LICENSE("GPL");
328 subsys_initcall(pcf50633_init);
329 module_exit(pcf50633_exit);