pcf50633-core.c 7.9 KB

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