pcf50633-core.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. buf1 += sprintf(buf1, "%*ph\n", (int)sizeof(dump), dump);
  95. }
  96. return buf1 - buf;
  97. }
  98. static DEVICE_ATTR(dump_regs, 0400, show_dump_regs, NULL);
  99. static ssize_t show_resume_reason(struct device *dev,
  100. struct device_attribute *attr, char *buf)
  101. {
  102. struct pcf50633 *pcf = dev_get_drvdata(dev);
  103. int n;
  104. n = sprintf(buf, "%02x%02x%02x%02x%02x\n",
  105. pcf->resume_reason[0],
  106. pcf->resume_reason[1],
  107. pcf->resume_reason[2],
  108. pcf->resume_reason[3],
  109. pcf->resume_reason[4]);
  110. return n;
  111. }
  112. static DEVICE_ATTR(resume_reason, 0400, show_resume_reason, NULL);
  113. static struct attribute *pcf_sysfs_entries[] = {
  114. &dev_attr_dump_regs.attr,
  115. &dev_attr_resume_reason.attr,
  116. NULL,
  117. };
  118. static struct attribute_group pcf_attr_group = {
  119. .name = NULL, /* put in device directory */
  120. .attrs = pcf_sysfs_entries,
  121. };
  122. static void
  123. pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
  124. struct platform_device **pdev)
  125. {
  126. int ret;
  127. *pdev = platform_device_alloc(name, -1);
  128. if (!*pdev) {
  129. dev_err(pcf->dev, "Failed to allocate %s\n", name);
  130. return;
  131. }
  132. (*pdev)->dev.parent = pcf->dev;
  133. ret = platform_device_add(*pdev);
  134. if (ret) {
  135. dev_err(pcf->dev, "Failed to register %s: %d\n", name, ret);
  136. platform_device_put(*pdev);
  137. *pdev = NULL;
  138. }
  139. }
  140. #ifdef CONFIG_PM_SLEEP
  141. static int pcf50633_suspend(struct device *dev)
  142. {
  143. struct i2c_client *client = to_i2c_client(dev);
  144. struct pcf50633 *pcf = i2c_get_clientdata(client);
  145. return pcf50633_irq_suspend(pcf);
  146. }
  147. static int pcf50633_resume(struct device *dev)
  148. {
  149. struct i2c_client *client = to_i2c_client(dev);
  150. struct pcf50633 *pcf = i2c_get_clientdata(client);
  151. return pcf50633_irq_resume(pcf);
  152. }
  153. #endif
  154. static SIMPLE_DEV_PM_OPS(pcf50633_pm, pcf50633_suspend, pcf50633_resume);
  155. static const struct regmap_config pcf50633_regmap_config = {
  156. .reg_bits = 8,
  157. .val_bits = 8,
  158. };
  159. static int pcf50633_probe(struct i2c_client *client,
  160. const struct i2c_device_id *ids)
  161. {
  162. struct pcf50633 *pcf;
  163. struct platform_device *pdev;
  164. struct pcf50633_platform_data *pdata = dev_get_platdata(&client->dev);
  165. int i, j, ret;
  166. int version, variant;
  167. if (!client->irq) {
  168. dev_err(&client->dev, "Missing IRQ\n");
  169. return -ENOENT;
  170. }
  171. pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL);
  172. if (!pcf)
  173. return -ENOMEM;
  174. i2c_set_clientdata(client, pcf);
  175. pcf->dev = &client->dev;
  176. pcf->pdata = pdata;
  177. mutex_init(&pcf->lock);
  178. pcf->regmap = devm_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", ret);
  182. return ret;
  183. }
  184. version = pcf50633_reg_read(pcf, 0);
  185. variant = pcf50633_reg_read(pcf, 1);
  186. if (version < 0 || variant < 0) {
  187. dev_err(pcf->dev, "Unable to probe pcf50633\n");
  188. ret = -ENODEV;
  189. return ret;
  190. }
  191. dev_info(pcf->dev, "Probed device version %d variant %d\n",
  192. version, variant);
  193. pcf50633_irq_init(pcf, client->irq);
  194. /* Create sub devices */
  195. pcf50633_client_dev_register(pcf, "pcf50633-input", &pcf->input_pdev);
  196. pcf50633_client_dev_register(pcf, "pcf50633-rtc", &pcf->rtc_pdev);
  197. pcf50633_client_dev_register(pcf, "pcf50633-mbc", &pcf->mbc_pdev);
  198. pcf50633_client_dev_register(pcf, "pcf50633-adc", &pcf->adc_pdev);
  199. pcf50633_client_dev_register(pcf, "pcf50633-backlight", &pcf->bl_pdev);
  200. for (i = 0; i < PCF50633_NUM_REGULATORS; i++) {
  201. pdev = platform_device_alloc("pcf50633-regulator", i);
  202. if (!pdev) {
  203. ret = -ENOMEM;
  204. goto err2;
  205. }
  206. pdev->dev.parent = pcf->dev;
  207. ret = platform_device_add_data(pdev, &pdata->reg_init_data[i],
  208. sizeof(pdata->reg_init_data[i]));
  209. if (ret)
  210. goto err;
  211. ret = platform_device_add(pdev);
  212. if (ret)
  213. goto err;
  214. pcf->regulator_pdev[i] = pdev;
  215. }
  216. ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
  217. if (ret)
  218. dev_warn(pcf->dev, "error creating sysfs entries\n");
  219. if (pdata->probe_done)
  220. pdata->probe_done(pcf);
  221. return 0;
  222. err:
  223. platform_device_put(pdev);
  224. err2:
  225. for (j = 0; j < i; j++)
  226. platform_device_put(pcf->regulator_pdev[j]);
  227. return ret;
  228. }
  229. static int pcf50633_remove(struct i2c_client *client)
  230. {
  231. struct pcf50633 *pcf = i2c_get_clientdata(client);
  232. int i;
  233. sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);
  234. pcf50633_irq_free(pcf);
  235. platform_device_unregister(pcf->input_pdev);
  236. platform_device_unregister(pcf->rtc_pdev);
  237. platform_device_unregister(pcf->mbc_pdev);
  238. platform_device_unregister(pcf->adc_pdev);
  239. platform_device_unregister(pcf->bl_pdev);
  240. for (i = 0; i < PCF50633_NUM_REGULATORS; i++)
  241. platform_device_unregister(pcf->regulator_pdev[i]);
  242. return 0;
  243. }
  244. static const struct i2c_device_id pcf50633_id_table[] = {
  245. {"pcf50633", 0x73},
  246. {/* end of list */}
  247. };
  248. MODULE_DEVICE_TABLE(i2c, pcf50633_id_table);
  249. static struct i2c_driver pcf50633_driver = {
  250. .driver = {
  251. .name = "pcf50633",
  252. .pm = &pcf50633_pm,
  253. },
  254. .id_table = pcf50633_id_table,
  255. .probe = pcf50633_probe,
  256. .remove = pcf50633_remove,
  257. };
  258. static int __init pcf50633_init(void)
  259. {
  260. return i2c_add_driver(&pcf50633_driver);
  261. }
  262. static void __exit pcf50633_exit(void)
  263. {
  264. i2c_del_driver(&pcf50633_driver);
  265. }
  266. MODULE_DESCRIPTION("I2C chip driver for NXP PCF50633 PMU");
  267. MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
  268. MODULE_LICENSE("GPL");
  269. subsys_initcall(pcf50633_init);
  270. module_exit(pcf50633_exit);