pcf50633-core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/i2c.h>
  23. #include <linux/pm.h>
  24. #include <linux/slab.h>
  25. #include <linux/mfd/pcf50633/core.h>
  26. static int __pcf50633_read(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
  27. {
  28. int ret;
  29. ret = i2c_smbus_read_i2c_block_data(pcf->i2c_client, reg,
  30. num, data);
  31. if (ret < 0)
  32. dev_err(pcf->dev, "Error reading %d regs at %d\n", num, reg);
  33. return ret;
  34. }
  35. static int __pcf50633_write(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
  36. {
  37. int ret;
  38. ret = i2c_smbus_write_i2c_block_data(pcf->i2c_client, reg,
  39. num, data);
  40. if (ret < 0)
  41. dev_err(pcf->dev, "Error writing %d regs at %d\n", num, reg);
  42. return ret;
  43. }
  44. /* Read a block of up to 32 regs */
  45. int pcf50633_read_block(struct pcf50633 *pcf, u8 reg,
  46. int nr_regs, u8 *data)
  47. {
  48. int ret;
  49. mutex_lock(&pcf->lock);
  50. ret = __pcf50633_read(pcf, reg, nr_regs, data);
  51. mutex_unlock(&pcf->lock);
  52. return ret;
  53. }
  54. EXPORT_SYMBOL_GPL(pcf50633_read_block);
  55. /* Write a block of up to 32 regs */
  56. int pcf50633_write_block(struct pcf50633 *pcf , u8 reg,
  57. int nr_regs, u8 *data)
  58. {
  59. int ret;
  60. mutex_lock(&pcf->lock);
  61. ret = __pcf50633_write(pcf, reg, nr_regs, data);
  62. mutex_unlock(&pcf->lock);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL_GPL(pcf50633_write_block);
  66. u8 pcf50633_reg_read(struct pcf50633 *pcf, u8 reg)
  67. {
  68. u8 val;
  69. mutex_lock(&pcf->lock);
  70. __pcf50633_read(pcf, reg, 1, &val);
  71. mutex_unlock(&pcf->lock);
  72. return val;
  73. }
  74. EXPORT_SYMBOL_GPL(pcf50633_reg_read);
  75. int pcf50633_reg_write(struct pcf50633 *pcf, u8 reg, u8 val)
  76. {
  77. int ret;
  78. mutex_lock(&pcf->lock);
  79. ret = __pcf50633_write(pcf, reg, 1, &val);
  80. mutex_unlock(&pcf->lock);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL_GPL(pcf50633_reg_write);
  84. int pcf50633_reg_set_bit_mask(struct pcf50633 *pcf, u8 reg, u8 mask, u8 val)
  85. {
  86. int ret;
  87. u8 tmp;
  88. val &= mask;
  89. mutex_lock(&pcf->lock);
  90. ret = __pcf50633_read(pcf, reg, 1, &tmp);
  91. if (ret < 0)
  92. goto out;
  93. tmp &= ~mask;
  94. tmp |= val;
  95. ret = __pcf50633_write(pcf, reg, 1, &tmp);
  96. out:
  97. mutex_unlock(&pcf->lock);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(pcf50633_reg_set_bit_mask);
  101. int pcf50633_reg_clear_bits(struct pcf50633 *pcf, u8 reg, u8 val)
  102. {
  103. int ret;
  104. u8 tmp;
  105. mutex_lock(&pcf->lock);
  106. ret = __pcf50633_read(pcf, reg, 1, &tmp);
  107. if (ret < 0)
  108. goto out;
  109. tmp &= ~val;
  110. ret = __pcf50633_write(pcf, reg, 1, &tmp);
  111. out:
  112. mutex_unlock(&pcf->lock);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL_GPL(pcf50633_reg_clear_bits);
  116. /* sysfs attributes */
  117. static ssize_t show_dump_regs(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct pcf50633 *pcf = dev_get_drvdata(dev);
  121. u8 dump[16];
  122. int n, n1, idx = 0;
  123. char *buf1 = buf;
  124. static u8 address_no_read[] = { /* must be ascending */
  125. PCF50633_REG_INT1,
  126. PCF50633_REG_INT2,
  127. PCF50633_REG_INT3,
  128. PCF50633_REG_INT4,
  129. PCF50633_REG_INT5,
  130. 0 /* terminator */
  131. };
  132. for (n = 0; n < 256; n += sizeof(dump)) {
  133. for (n1 = 0; n1 < sizeof(dump); n1++)
  134. if (n == address_no_read[idx]) {
  135. idx++;
  136. dump[n1] = 0x00;
  137. } else
  138. dump[n1] = pcf50633_reg_read(pcf, n + n1);
  139. hex_dump_to_buffer(dump, sizeof(dump), 16, 1, buf1, 128, 0);
  140. buf1 += strlen(buf1);
  141. *buf1++ = '\n';
  142. *buf1 = '\0';
  143. }
  144. return buf1 - buf;
  145. }
  146. static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
  147. static ssize_t show_resume_reason(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct pcf50633 *pcf = dev_get_drvdata(dev);
  151. int n;
  152. n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
  153. pcf->resume_reason[0],
  154. pcf->resume_reason[1],
  155. pcf->resume_reason[2],
  156. pcf->resume_reason[3],
  157. pcf->resume_reason[4]);
  158. return n;
  159. }
  160. static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
  161. static struct attribute *pcf_sysfs_entries[] = {
  162. &dev_attr_dump_regs.attr,
  163. &dev_attr_resume_reason.attr,
  164. NULL,
  165. };
  166. static struct attribute_group pcf_attr_group = {
  167. .name = NULL, /* put in device directory */
  168. .attrs = pcf_sysfs_entries,
  169. };
  170. static void
  171. pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
  172. struct platform_device **pdev)
  173. {
  174. int ret;
  175. *pdev = platform_device_alloc(name, -1);
  176. if (!*pdev) {
  177. dev_err(pcf->dev, "Falied to allocate %s\n", name);
  178. return;
  179. }
  180. (*pdev)->dev.parent = pcf->dev;
  181. ret = platform_device_add(*pdev);
  182. if (ret) {
  183. dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
  184. platform_device_put(*pdev);
  185. *pdev = NULL;
  186. }
  187. }
  188. #ifdef CONFIG_PM_SLEEP
  189. static int pcf50633_suspend(struct device *dev)
  190. {
  191. struct i2c_client *client = to_i2c_client(dev);
  192. struct pcf50633 *pcf = i2c_get_clientdata(client);
  193. return pcf50633_irq_suspend(pcf);
  194. }
  195. static int pcf50633_resume(struct device *dev)
  196. {
  197. struct i2c_client *client = to_i2c_client(dev);
  198. struct pcf50633 *pcf = i2c_get_clientdata(client);
  199. return pcf50633_irq_resume(pcf);
  200. }
  201. #endif
  202. static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
  203. static int __devinit pcf50633_probe(struct i2c_client *client,
  204. const struct i2c_device_id *ids)
  205. {
  206. struct pcf50633 *pcf;
  207. struct pcf50633_platform_data *pdata = client->dev.platform_data;
  208. int i, ret;
  209. int version, variant;
  210. if (!client->irq) {
  211. dev_err(&client->dev, "Missing IRQ\n");
  212. return -ENOENT;
  213. }
  214. pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
  215. if (!pcf)
  216. return -ENOMEM;
  217. pcf->pdata = pdata;
  218. mutex_init(&pcf->lock);
  219. i2c_set_clientdata(client, pcf);
  220. pcf->dev = &client->dev;
  221. pcf->i2c_client = client;
  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. goto err_free;
  228. }
  229. dev_info(pcf->dev, "Probed device version %d variant %d\n",
  230. version, variant);
  231. pcf50633_irq_init(pcf, client->irq);
  232. /* Create sub devices */
  233. pcf50633_client_dev_register(pcf, "pcf50633-input",
  234. &pcf->input_pdev);
  235. pcf50633_client_dev_register(pcf, "pcf50633-rtc",
  236. &pcf->rtc_pdev);
  237. pcf50633_client_dev_register(pcf, "pcf50633-mbc",
  238. &pcf->mbc_pdev);
  239. pcf50633_client_dev_register(pcf, "pcf50633-adc",
  240. &pcf->adc_pdev);
  241. pcf50633_client_dev_register(pcf, "pcf50633-backlight",
  242. &pcf->bl_pdev);
  243. for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
  244. struct platform_device *pdev;
  245. pdev = platform_device_alloc("pcf50633-regltr", i);
  246. if (!pdev) {
  247. dev_err(pcf->dev, "Cannot create regulator %d\n", i);
  248. continue;
  249. }
  250. pdev->dev.parent = pcf->dev;
  251. platform_device_add_data(pdev, &pdata->reg_init_data[i],
  252. sizeof(pdata->reg_init_data[i]));
  253. pcf->regulator_pdev[i] = pdev;
  254. platform_device_add(pdev);
  255. }
  256. ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
  257. if (ret)
  258. dev_err(pcf->dev, "error creating sysfs entries\n");
  259. if (pdata->probe_done)
  260. pdata->probe_done(pcf);
  261. return 0;
  262. err_free:
  263. kfree(pcf);
  264. return ret;
  265. }
  266. static int __devexit pcf50633_remove(struct i2c_client *client)
  267. {
  268. struct pcf50633 *pcf = i2c_get_clientdata(client);
  269. int i;
  270. sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
  271. pcf50633_irq_free(pcf);
  272. platform_device_unregister(pcf->input_pdev);
  273. platform_device_unregister(pcf->rtc_pdev);
  274. platform_device_unregister(pcf->mbc_pdev);
  275. platform_device_unregister(pcf->adc_pdev);
  276. platform_device_unregister(pcf->bl_pdev);
  277. for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
  278. platform_device_unregister(pcf->regulator_pdev[i]);
  279. kfree(pcf);
  280. return 0;
  281. }
  282. static const struct i2c_device_id pcf50633_id_table[] = {
  283. {"pcf50633", 0x73},
  284. {/* end of list */}
  285. };
  286. MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
  287. static struct i2c_driver pcf50633_driver = {
  288. .driver = {
  289. .name = "pcf50633",
  290. .pm = &pcf50633_pm,
  291. },
  292. .id_table = pcf50633_id_table,
  293. .probe = pcf50633_probe,
  294. .remove = __devexit_p(pcf50633_remove),
  295. };
  296. static int __init pcf50633_init(void)
  297. {
  298. return i2c_add_driver(&pcf50633_driver);
  299. }
  300. static void __exit pcf50633_exit(void)
  301. {
  302. i2c_del_driver(&pcf50633_driver);
  303. }
  304. MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
  305. MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
  306. MODULE_LICENSE("GPL");
  307. subsys_initcall(pcf50633_init);
  308. module_exit(pcf50633_exit);