da9063-core.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * da9063-core.c: Device access for Dialog DA9063 modules
  3. *
  4. * Copyright 2012 Dialog Semiconductors Ltd.
  5. * Copyright 2013 Philipp Zabel, Pengutronix
  6. *
  7. * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>,
  8. * Michal Hajduk <michal.hajduk@diasemi.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/device.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mutex.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/da9063/core.h>
  27. #include <linux/mfd/da9063/pdata.h>
  28. #include <linux/mfd/da9063/registers.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/kthread.h>
  31. #include <linux/uaccess.h>
  32. static struct resource da9063_regulators_resources[] = {
  33. {
  34. .name = "LDO_LIM",
  35. .start = DA9063_IRQ_LDO_LIM,
  36. .end = DA9063_IRQ_LDO_LIM,
  37. .flags = IORESOURCE_IRQ,
  38. },
  39. };
  40. static struct resource da9063_rtc_resources[] = {
  41. {
  42. .name = "ALARM",
  43. .start = DA9063_IRQ_ALARM,
  44. .end = DA9063_IRQ_ALARM,
  45. .flags = IORESOURCE_IRQ,
  46. },
  47. {
  48. .name = "TICK",
  49. .start = DA9063_IRQ_TICK,
  50. .end = DA9063_IRQ_TICK,
  51. .flags = IORESOURCE_IRQ,
  52. }
  53. };
  54. static struct resource da9063_onkey_resources[] = {
  55. {
  56. .name = "ONKEY",
  57. .start = DA9063_IRQ_ONKEY,
  58. .end = DA9063_IRQ_ONKEY,
  59. .flags = IORESOURCE_IRQ,
  60. },
  61. };
  62. static struct resource da9063_hwmon_resources[] = {
  63. {
  64. .start = DA9063_IRQ_ADC_RDY,
  65. .end = DA9063_IRQ_ADC_RDY,
  66. .flags = IORESOURCE_IRQ,
  67. },
  68. };
  69. static const struct mfd_cell da9063_devs[] = {
  70. {
  71. .name = DA9063_DRVNAME_REGULATORS,
  72. .num_resources = ARRAY_SIZE(da9063_regulators_resources),
  73. .resources = da9063_regulators_resources,
  74. },
  75. {
  76. .name = DA9063_DRVNAME_LEDS,
  77. },
  78. {
  79. .name = DA9063_DRVNAME_WATCHDOG,
  80. .of_compatible = "dlg,da9063-watchdog",
  81. },
  82. {
  83. .name = DA9063_DRVNAME_HWMON,
  84. .num_resources = ARRAY_SIZE(da9063_hwmon_resources),
  85. .resources = da9063_hwmon_resources,
  86. },
  87. {
  88. .name = DA9063_DRVNAME_ONKEY,
  89. .num_resources = ARRAY_SIZE(da9063_onkey_resources),
  90. .resources = da9063_onkey_resources,
  91. .of_compatible = "dlg,da9063-onkey",
  92. },
  93. {
  94. .name = DA9063_DRVNAME_RTC,
  95. .num_resources = ARRAY_SIZE(da9063_rtc_resources),
  96. .resources = da9063_rtc_resources,
  97. .of_compatible = "dlg,da9063-rtc",
  98. },
  99. {
  100. .name = DA9063_DRVNAME_VIBRATION,
  101. },
  102. };
  103. static int da9063_clear_fault_log(struct da9063 *da9063)
  104. {
  105. int ret = 0;
  106. int fault_log = 0;
  107. ret = regmap_read(da9063->regmap, DA9063_REG_FAULT_LOG, &fault_log);
  108. if (ret < 0) {
  109. dev_err(da9063->dev, "Cannot read FAULT_LOG.\n");
  110. return -EIO;
  111. }
  112. if (fault_log) {
  113. if (fault_log & DA9063_TWD_ERROR)
  114. dev_dbg(da9063->dev,
  115. "Fault log entry detected: DA9063_TWD_ERROR\n");
  116. if (fault_log & DA9063_POR)
  117. dev_dbg(da9063->dev,
  118. "Fault log entry detected: DA9063_POR\n");
  119. if (fault_log & DA9063_VDD_FAULT)
  120. dev_dbg(da9063->dev,
  121. "Fault log entry detected: DA9063_VDD_FAULT\n");
  122. if (fault_log & DA9063_VDD_START)
  123. dev_dbg(da9063->dev,
  124. "Fault log entry detected: DA9063_VDD_START\n");
  125. if (fault_log & DA9063_TEMP_CRIT)
  126. dev_dbg(da9063->dev,
  127. "Fault log entry detected: DA9063_TEMP_CRIT\n");
  128. if (fault_log & DA9063_KEY_RESET)
  129. dev_dbg(da9063->dev,
  130. "Fault log entry detected: DA9063_KEY_RESET\n");
  131. if (fault_log & DA9063_NSHUTDOWN)
  132. dev_dbg(da9063->dev,
  133. "Fault log entry detected: DA9063_NSHUTDOWN\n");
  134. if (fault_log & DA9063_WAIT_SHUT)
  135. dev_dbg(da9063->dev,
  136. "Fault log entry detected: DA9063_WAIT_SHUT\n");
  137. }
  138. ret = regmap_write(da9063->regmap,
  139. DA9063_REG_FAULT_LOG,
  140. fault_log);
  141. if (ret < 0)
  142. dev_err(da9063->dev,
  143. "Cannot reset FAULT_LOG values %d\n", ret);
  144. return ret;
  145. }
  146. int da9063_device_init(struct da9063 *da9063, unsigned int irq)
  147. {
  148. struct da9063_pdata *pdata = da9063->dev->platform_data;
  149. int model, variant_id, variant_code;
  150. int ret;
  151. ret = da9063_clear_fault_log(da9063);
  152. if (ret < 0)
  153. dev_err(da9063->dev, "Cannot clear fault log\n");
  154. if (pdata) {
  155. da9063->flags = pdata->flags;
  156. da9063->irq_base = pdata->irq_base;
  157. } else {
  158. da9063->flags = 0;
  159. da9063->irq_base = -1;
  160. }
  161. da9063->chip_irq = irq;
  162. if (pdata && pdata->init != NULL) {
  163. ret = pdata->init(da9063);
  164. if (ret != 0) {
  165. dev_err(da9063->dev,
  166. "Platform initialization failed.\n");
  167. return ret;
  168. }
  169. }
  170. ret = regmap_read(da9063->regmap, DA9063_REG_CHIP_ID, &model);
  171. if (ret < 0) {
  172. dev_err(da9063->dev, "Cannot read chip model id.\n");
  173. return -EIO;
  174. }
  175. if (model != PMIC_DA9063) {
  176. dev_err(da9063->dev, "Invalid chip model id: 0x%02x\n", model);
  177. return -ENODEV;
  178. }
  179. ret = regmap_read(da9063->regmap, DA9063_REG_CHIP_VARIANT, &variant_id);
  180. if (ret < 0) {
  181. dev_err(da9063->dev, "Cannot read chip variant id.\n");
  182. return -EIO;
  183. }
  184. variant_code = variant_id >> DA9063_CHIP_VARIANT_SHIFT;
  185. dev_info(da9063->dev,
  186. "Device detected (chip-ID: 0x%02X, var-ID: 0x%02X)\n",
  187. model, variant_id);
  188. if (variant_code < PMIC_DA9063_BB && variant_code != PMIC_DA9063_AD) {
  189. dev_err(da9063->dev,
  190. "Cannot support variant code: 0x%02X\n", variant_code);
  191. return -ENODEV;
  192. }
  193. da9063->model = model;
  194. da9063->variant_code = variant_code;
  195. ret = da9063_irq_init(da9063);
  196. if (ret) {
  197. dev_err(da9063->dev, "Cannot initialize interrupts.\n");
  198. return ret;
  199. }
  200. da9063->irq_base = regmap_irq_chip_get_base(da9063->regmap_irq);
  201. ret = mfd_add_devices(da9063->dev, -1, da9063_devs,
  202. ARRAY_SIZE(da9063_devs), NULL, da9063->irq_base,
  203. NULL);
  204. if (ret)
  205. dev_err(da9063->dev, "Cannot add MFD cells\n");
  206. return ret;
  207. }
  208. void da9063_device_exit(struct da9063 *da9063)
  209. {
  210. mfd_remove_devices(da9063->dev);
  211. da9063_irq_exit(da9063);
  212. }
  213. MODULE_DESCRIPTION("PMIC driver for Dialog DA9063");
  214. MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>, Michal Hajduk <michal.hajduk@diasemi.com>");
  215. MODULE_LICENSE("GPL");