Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 1 | /* NXP PCF50633 Power Management Unit (PMU) driver |
| 2 | * |
| 3 | * (C) 2006-2008 by Openmoko, Inc. |
| 4 | * Author: Harald Welte <laforge@openmoko.org> |
| 5 | * Balaji Rao <balajirrao@openmoko.org> |
| 6 | * All rights reserved. |
| 7 | * |
| 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. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/sysfs.h> |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 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> |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 24 | #include <linux/pm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 26 | #include <linux/regmap.h> |
| 27 | #include <linux/err.h> |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 28 | |
| 29 | #include <linux/mfd/pcf50633/core.h> |
| 30 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 31 | /* Read a block of up to 32 regs */ |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 32 | int pcf50633_read_block(struct pcf50633 *pcf, u8 reg, |
| 33 | int nr_regs, u8 *data) |
| 34 | { |
| 35 | int ret; |
| 36 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 37 | ret = regmap_raw_read(pcf->regmap, reg, data, nr_regs); |
| 38 | if (ret != 0) |
| 39 | return ret; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 40 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 41 | return nr_regs; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 42 | } |
| 43 | EXPORT_SYMBOL_GPL(pcf50633_read_block); |
| 44 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 45 | /* Write a block of up to 32 regs */ |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 46 | int pcf50633_write_block(struct pcf50633 *pcf , u8 reg, |
| 47 | int nr_regs, u8 *data) |
| 48 | { |
| 49 | int ret; |
| 50 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 51 | ret = regmap_raw_write(pcf->regmap, reg, data, nr_regs); |
| 52 | if (ret != 0) |
| 53 | return ret; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 54 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 55 | return nr_regs; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 56 | } |
| 57 | EXPORT_SYMBOL_GPL(pcf50633_write_block); |
| 58 | |
| 59 | u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg) |
| 60 | { |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 61 | unsigned int val; |
| 62 | int ret; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 63 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 64 | ret = regmap_read(pcf->regmap, reg, &val); |
| 65 | if (ret < 0) |
| 66 | return -1; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 67 | |
| 68 | return val; |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(pcf50633_reg_read); |
| 71 | |
| 72 | int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val) |
| 73 | { |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 74 | return regmap_write(pcf->regmap, reg, val); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL_GPL(pcf50633_reg_write); |
| 77 | |
| 78 | int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val) |
| 79 | { |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 80 | return regmap_update_bits(pcf->regmap, reg, mask, val); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask); |
| 83 | |
| 84 | int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val) |
| 85 | { |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 86 | return regmap_update_bits(pcf->regmap, reg, val, 0); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 87 | } |
| 88 | EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits); |
| 89 | |
| 90 | /* sysfs attributes */ |
| 91 | static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr, |
| 92 | char *buf) |
| 93 | { |
| 94 | struct pcf50633 *pcf = dev_get_drvdata(dev); |
| 95 | u8 dump[16]; |
| 96 | int n, n1, idx = 0; |
| 97 | char *buf1 = buf; |
| 98 | static u8 address_no_read[] = { /* must be ascending */ |
| 99 | PCF50633_REG_INT1, |
| 100 | PCF50633_REG_INT2, |
| 101 | PCF50633_REG_INT3, |
| 102 | PCF50633_REG_INT4, |
| 103 | PCF50633_REG_INT5, |
| 104 | 0 /* terminator */ |
| 105 | }; |
| 106 | |
| 107 | for (n = 0; n < 256; n += sizeof(dump)) { |
| 108 | for (n1 = 0; n1 < sizeof(dump); n1++) |
| 109 | if (n == address_no_read[idx]) { |
| 110 | idx++; |
| 111 | dump[n1] = 0x00; |
| 112 | } else |
| 113 | dump[n1] = pcf50633_reg_read(pcf, n + n1); |
| 114 | |
| 115 | hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0); |
| 116 | buf1 += strlen(buf1); |
| 117 | *buf1++ = '\n'; |
| 118 | *buf1 = '\0'; |
| 119 | } |
| 120 | |
| 121 | return buf1 - buf; |
| 122 | } |
| 123 | static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL); |
| 124 | |
| 125 | static ssize_t show_resume_reason(struct device *dev, |
| 126 | struct device_attribute *attr, char *buf) |
| 127 | { |
| 128 | struct pcf50633 *pcf = dev_get_drvdata(dev); |
| 129 | int n; |
| 130 | |
| 131 | n = sprintf(buf, "%02x%02x%02x%02x%02x\n", |
| 132 | pcf->resume_reason[0], |
| 133 | pcf->resume_reason[1], |
| 134 | pcf->resume_reason[2], |
| 135 | pcf->resume_reason[3], |
| 136 | pcf->resume_reason[4]); |
| 137 | |
| 138 | return n; |
| 139 | } |
| 140 | static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL); |
| 141 | |
| 142 | static struct attribute *pcf_sysfs_entries[] = { |
| 143 | &dev_attr_dump_regs.attr, |
| 144 | &dev_attr_resume_reason.attr, |
| 145 | NULL, |
| 146 | }; |
| 147 | |
| 148 | static struct attribute_group pcf_attr_group = { |
| 149 | .name = NULL, /* put in device directory */ |
| 150 | .attrs = pcf_sysfs_entries, |
| 151 | }; |
| 152 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 153 | static void |
| 154 | pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name, |
| 155 | struct platform_device **pdev) |
| 156 | { |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 157 | int ret; |
| 158 | |
| 159 | *pdev = platform_device_alloc(name, -1); |
| 160 | if (!*pdev) { |
| 161 | dev_err(pcf->dev, "Falied to allocate %s\n", name); |
| 162 | return; |
| 163 | } |
| 164 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 165 | (*pdev)->dev.parent = pcf->dev; |
| 166 | |
| 167 | ret = platform_device_add(*pdev); |
| 168 | if (ret) { |
| 169 | dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret); |
| 170 | platform_device_put(*pdev); |
| 171 | *pdev = NULL; |
| 172 | } |
| 173 | } |
| 174 | |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 175 | #ifdef CONFIG_PM_SLEEP |
| 176 | static int pcf50633_suspend(struct device *dev) |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 177 | { |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 178 | struct i2c_client *client = to_i2c_client(dev); |
| 179 | struct pcf50633 *pcf = i2c_get_clientdata(client); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 180 | |
Lars-Peter Clausen | 380c09f | 2010-05-12 02:10:56 +0200 | [diff] [blame] | 181 | return pcf50633_irq_suspend(pcf); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 184 | static int pcf50633_resume(struct device *dev) |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 185 | { |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 186 | struct i2c_client *client = to_i2c_client(dev); |
| 187 | struct pcf50633 *pcf = i2c_get_clientdata(client); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 188 | |
Lars-Peter Clausen | 380c09f | 2010-05-12 02:10:56 +0200 | [diff] [blame] | 189 | return pcf50633_irq_resume(pcf); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 190 | } |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 191 | #endif |
| 192 | |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 193 | static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume); |
| 194 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 195 | static struct regmap_config pcf50633_regmap_config = { |
| 196 | .reg_bits = 8, |
| 197 | .val_bits = 8, |
| 198 | }; |
| 199 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 200 | static int __devinit pcf50633_probe(struct i2c_client *client, |
| 201 | const struct i2c_device_id *ids) |
| 202 | { |
| 203 | struct pcf50633 *pcf; |
| 204 | struct pcf50633_platform_data *pdata = client->dev.platform_data; |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 205 | int i, ret; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 206 | int version, variant; |
| 207 | |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 208 | if (!client->irq) { |
| 209 | dev_err(&client->dev, "Missing IRQ\n"); |
| 210 | return -ENOENT; |
| 211 | } |
| 212 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 213 | pcf = kzalloc(sizeof(*pcf), GFP_KERNEL); |
| 214 | if (!pcf) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | pcf->pdata = pdata; |
| 218 | |
| 219 | mutex_init(&pcf->lock); |
| 220 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 221 | pcf->regmap = regmap_init_i2c(client, &pcf50633_regmap_config); |
| 222 | if (IS_ERR(pcf->regmap)) { |
| 223 | ret = PTR_ERR(pcf->regmap); |
| 224 | dev_err(pcf->dev, "Failed to allocate register map: %d\n", |
| 225 | ret); |
| 226 | goto err_free; |
| 227 | } |
| 228 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 229 | i2c_set_clientdata(client, pcf); |
| 230 | pcf->dev = &client->dev; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 231 | |
| 232 | version = pcf50633_reg_read(pcf, 0); |
| 233 | variant = pcf50633_reg_read(pcf, 1); |
| 234 | if (version < 0 || variant < 0) { |
| 235 | dev_err(pcf->dev, "Unable to probe pcf50633\n"); |
| 236 | ret = -ENODEV; |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 237 | goto err_regmap; |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | dev_info(pcf->dev, "Probed device version %d variant %d\n", |
| 241 | version, variant); |
| 242 | |
Lars-Peter Clausen | 380c09f | 2010-05-12 02:10:56 +0200 | [diff] [blame] | 243 | pcf50633_irq_init(pcf, client->irq); |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 244 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 245 | /* Create sub devices */ |
| 246 | pcf50633_client_dev_register(pcf, "pcf50633-input", |
| 247 | &pcf->input_pdev); |
| 248 | pcf50633_client_dev_register(pcf, "pcf50633-rtc", |
| 249 | &pcf->rtc_pdev); |
| 250 | pcf50633_client_dev_register(pcf, "pcf50633-mbc", |
| 251 | &pcf->mbc_pdev); |
| 252 | pcf50633_client_dev_register(pcf, "pcf50633-adc", |
| 253 | &pcf->adc_pdev); |
Lars-Peter Clausen | f5bf403 | 2010-05-12 02:44:33 +0200 | [diff] [blame] | 254 | pcf50633_client_dev_register(pcf, "pcf50633-backlight", |
| 255 | &pcf->bl_pdev); |
| 256 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 257 | |
| 258 | for (i = 0; i < PCF50633_NUM_REGULATORS; i++) { |
| 259 | struct platform_device *pdev; |
| 260 | |
| 261 | pdev = platform_device_alloc("pcf50633-regltr", i); |
| 262 | if (!pdev) { |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 263 | dev_err(pcf->dev, "Cannot create regulator %d\n", i); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 264 | continue; |
| 265 | } |
| 266 | |
| 267 | pdev->dev.parent = pcf->dev; |
Lars-Peter Clausen | bbb2e49 | 2009-10-14 02:12:35 +0400 | [diff] [blame] | 268 | platform_device_add_data(pdev, &pdata->reg_init_data[i], |
| 269 | sizeof(pdata->reg_init_data[i])); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 270 | pcf->regulator_pdev[i] = pdev; |
| 271 | |
| 272 | platform_device_add(pdev); |
| 273 | } |
| 274 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 275 | ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group); |
| 276 | if (ret) |
| 277 | dev_err(pcf->dev, "error creating sysfs entries\n"); |
| 278 | |
| 279 | if (pdata->probe_done) |
| 280 | pdata->probe_done(pcf); |
| 281 | |
| 282 | return 0; |
| 283 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 284 | err_regmap: |
| 285 | regmap_exit(pcf->regmap); |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 286 | err_free: |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 287 | kfree(pcf); |
Lars-Peter Clausen | 24213ae | 2009-10-08 00:24:54 +0200 | [diff] [blame] | 288 | |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int __devexit pcf50633_remove(struct i2c_client *client) |
| 293 | { |
| 294 | struct pcf50633 *pcf = i2c_get_clientdata(client); |
| 295 | int i; |
| 296 | |
Axel Lin | 8220fe4 | 2010-10-20 16:56:59 +0800 | [diff] [blame] | 297 | sysfs_remove_group(&client->dev.kobj, &pcf_attr_group); |
Lars-Peter Clausen | 380c09f | 2010-05-12 02:10:56 +0200 | [diff] [blame] | 298 | pcf50633_irq_free(pcf); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 299 | |
| 300 | platform_device_unregister(pcf->input_pdev); |
| 301 | platform_device_unregister(pcf->rtc_pdev); |
| 302 | platform_device_unregister(pcf->mbc_pdev); |
| 303 | platform_device_unregister(pcf->adc_pdev); |
Axel Lin | 8220fe4 | 2010-10-20 16:56:59 +0800 | [diff] [blame] | 304 | platform_device_unregister(pcf->bl_pdev); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 305 | |
| 306 | for (i = 0; i < PCF50633_NUM_REGULATORS; i++) |
| 307 | platform_device_unregister(pcf->regulator_pdev[i]); |
| 308 | |
Mark Brown | 6e3ad11 | 2011-08-08 17:04:40 +0900 | [diff] [blame] | 309 | regmap_exit(pcf->regmap); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 310 | kfree(pcf); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Axel Lin | 1206552 | 2011-03-23 20:54:17 +0800 | [diff] [blame] | 315 | static const struct i2c_device_id pcf50633_id_table[] = { |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 316 | {"pcf50633", 0x73}, |
Jean Delvare | 8915e54 | 2009-02-17 09:07:02 +0100 | [diff] [blame] | 317 | {/* end of list */} |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 318 | }; |
Axel Lin | 7679089 | 2011-02-28 14:33:12 +0800 | [diff] [blame] | 319 | MODULE_DEVICE_TABLE(i2c, pcf50633_id_table); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 320 | |
| 321 | static struct i2c_driver pcf50633_driver = { |
| 322 | .driver = { |
| 323 | .name = "pcf50633", |
Mark Brown | 939941d | 2011-01-20 21:52:42 +0000 | [diff] [blame] | 324 | .pm = &pcf50633_pm, |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 325 | }, |
| 326 | .id_table = pcf50633_id_table, |
| 327 | .probe = pcf50633_probe, |
| 328 | .remove = __devexit_p(pcf50633_remove), |
| 329 | }; |
| 330 | |
| 331 | static int __init pcf50633_init(void) |
| 332 | { |
| 333 | return i2c_add_driver(&pcf50633_driver); |
| 334 | } |
| 335 | |
| 336 | static void __exit pcf50633_exit(void) |
| 337 | { |
| 338 | i2c_del_driver(&pcf50633_driver); |
| 339 | } |
| 340 | |
| 341 | MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU"); |
| 342 | MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>"); |
| 343 | MODULE_LICENSE("GPL"); |
| 344 | |
Samuel Ortiz | 2021de8 | 2009-06-15 18:04:54 +0200 | [diff] [blame] | 345 | subsys_initcall(pcf50633_init); |
Balaji Rao | f52046b | 2009-01-09 01:49:01 +0100 | [diff] [blame] | 346 | module_exit(pcf50633_exit); |