da9063-i2c.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* da9063-i2c.c: Interrupt support for Dialog DA9063
  2. *
  3. * Copyright 2012 Dialog Semiconductor Ltd.
  4. * Copyright 2013 Philipp Zabel, Pengutronix
  5. *
  6. * Author: Krystian Garbaciak, Dialog Semiconductor
  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/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/da9063/core.h>
  23. #include <linux/mfd/da9063/pdata.h>
  24. #include <linux/mfd/da9063/registers.h>
  25. #include <linux/of.h>
  26. #include <linux/regulator/of_regulator.h>
  27. static const struct regmap_range da9063_ad_readable_ranges[] = {
  28. {
  29. .range_min = DA9063_REG_PAGE_CON,
  30. .range_max = DA9063_AD_REG_SECOND_D,
  31. }, {
  32. .range_min = DA9063_REG_SEQ,
  33. .range_max = DA9063_REG_ID_32_31,
  34. }, {
  35. .range_min = DA9063_REG_SEQ_A,
  36. .range_max = DA9063_REG_AUTO3_LOW,
  37. }, {
  38. .range_min = DA9063_REG_T_OFFSET,
  39. .range_max = DA9063_AD_REG_GP_ID_19,
  40. }, {
  41. .range_min = DA9063_REG_CHIP_ID,
  42. .range_max = DA9063_REG_CHIP_VARIANT,
  43. },
  44. };
  45. static const struct regmap_range da9063_ad_writeable_ranges[] = {
  46. {
  47. .range_min = DA9063_REG_PAGE_CON,
  48. .range_max = DA9063_REG_PAGE_CON,
  49. }, {
  50. .range_min = DA9063_REG_FAULT_LOG,
  51. .range_max = DA9063_REG_VSYS_MON,
  52. }, {
  53. .range_min = DA9063_REG_COUNT_S,
  54. .range_max = DA9063_AD_REG_ALARM_Y,
  55. }, {
  56. .range_min = DA9063_REG_SEQ,
  57. .range_max = DA9063_REG_ID_32_31,
  58. }, {
  59. .range_min = DA9063_REG_SEQ_A,
  60. .range_max = DA9063_REG_AUTO3_LOW,
  61. }, {
  62. .range_min = DA9063_REG_CONFIG_I,
  63. .range_max = DA9063_AD_REG_MON_REG_4,
  64. }, {
  65. .range_min = DA9063_AD_REG_GP_ID_0,
  66. .range_max = DA9063_AD_REG_GP_ID_19,
  67. },
  68. };
  69. static const struct regmap_range da9063_ad_volatile_ranges[] = {
  70. {
  71. .range_min = DA9063_REG_PAGE_CON,
  72. .range_max = DA9063_REG_EVENT_D,
  73. }, {
  74. .range_min = DA9063_REG_CONTROL_A,
  75. .range_max = DA9063_REG_CONTROL_B,
  76. }, {
  77. .range_min = DA9063_REG_CONTROL_E,
  78. .range_max = DA9063_REG_CONTROL_F,
  79. }, {
  80. .range_min = DA9063_REG_BCORE2_CONT,
  81. .range_max = DA9063_REG_LDO11_CONT,
  82. }, {
  83. .range_min = DA9063_REG_DVC_1,
  84. .range_max = DA9063_REG_ADC_MAN,
  85. }, {
  86. .range_min = DA9063_REG_ADC_RES_L,
  87. .range_max = DA9063_AD_REG_SECOND_D,
  88. }, {
  89. .range_min = DA9063_REG_SEQ,
  90. .range_max = DA9063_REG_SEQ,
  91. }, {
  92. .range_min = DA9063_REG_EN_32K,
  93. .range_max = DA9063_REG_EN_32K,
  94. }, {
  95. .range_min = DA9063_AD_REG_MON_REG_5,
  96. .range_max = DA9063_AD_REG_MON_REG_6,
  97. },
  98. };
  99. static const struct regmap_access_table da9063_ad_readable_table = {
  100. .yes_ranges = da9063_ad_readable_ranges,
  101. .n_yes_ranges = ARRAY_SIZE(da9063_ad_readable_ranges),
  102. };
  103. static const struct regmap_access_table da9063_ad_writeable_table = {
  104. .yes_ranges = da9063_ad_writeable_ranges,
  105. .n_yes_ranges = ARRAY_SIZE(da9063_ad_writeable_ranges),
  106. };
  107. static const struct regmap_access_table da9063_ad_volatile_table = {
  108. .yes_ranges = da9063_ad_volatile_ranges,
  109. .n_yes_ranges = ARRAY_SIZE(da9063_ad_volatile_ranges),
  110. };
  111. static const struct regmap_range da9063_bb_readable_ranges[] = {
  112. {
  113. .range_min = DA9063_REG_PAGE_CON,
  114. .range_max = DA9063_BB_REG_SECOND_D,
  115. }, {
  116. .range_min = DA9063_REG_SEQ,
  117. .range_max = DA9063_REG_ID_32_31,
  118. }, {
  119. .range_min = DA9063_REG_SEQ_A,
  120. .range_max = DA9063_REG_AUTO3_LOW,
  121. }, {
  122. .range_min = DA9063_REG_T_OFFSET,
  123. .range_max = DA9063_BB_REG_GP_ID_19,
  124. }, {
  125. .range_min = DA9063_REG_CHIP_ID,
  126. .range_max = DA9063_REG_CHIP_VARIANT,
  127. },
  128. };
  129. static const struct regmap_range da9063_bb_writeable_ranges[] = {
  130. {
  131. .range_min = DA9063_REG_PAGE_CON,
  132. .range_max = DA9063_REG_PAGE_CON,
  133. }, {
  134. .range_min = DA9063_REG_FAULT_LOG,
  135. .range_max = DA9063_REG_VSYS_MON,
  136. }, {
  137. .range_min = DA9063_REG_COUNT_S,
  138. .range_max = DA9063_BB_REG_ALARM_Y,
  139. }, {
  140. .range_min = DA9063_REG_SEQ,
  141. .range_max = DA9063_REG_ID_32_31,
  142. }, {
  143. .range_min = DA9063_REG_SEQ_A,
  144. .range_max = DA9063_REG_AUTO3_LOW,
  145. }, {
  146. .range_min = DA9063_REG_CONFIG_I,
  147. .range_max = DA9063_BB_REG_MON_REG_4,
  148. }, {
  149. .range_min = DA9063_BB_REG_GP_ID_0,
  150. .range_max = DA9063_BB_REG_GP_ID_19,
  151. },
  152. };
  153. static const struct regmap_range da9063_bb_volatile_ranges[] = {
  154. {
  155. .range_min = DA9063_REG_PAGE_CON,
  156. .range_max = DA9063_REG_EVENT_D,
  157. }, {
  158. .range_min = DA9063_REG_CONTROL_A,
  159. .range_max = DA9063_REG_CONTROL_B,
  160. }, {
  161. .range_min = DA9063_REG_CONTROL_E,
  162. .range_max = DA9063_REG_CONTROL_F,
  163. }, {
  164. .range_min = DA9063_REG_BCORE2_CONT,
  165. .range_max = DA9063_REG_LDO11_CONT,
  166. }, {
  167. .range_min = DA9063_REG_DVC_1,
  168. .range_max = DA9063_REG_ADC_MAN,
  169. }, {
  170. .range_min = DA9063_REG_ADC_RES_L,
  171. .range_max = DA9063_BB_REG_SECOND_D,
  172. }, {
  173. .range_min = DA9063_REG_SEQ,
  174. .range_max = DA9063_REG_SEQ,
  175. }, {
  176. .range_min = DA9063_REG_EN_32K,
  177. .range_max = DA9063_REG_EN_32K,
  178. }, {
  179. .range_min = DA9063_BB_REG_MON_REG_5,
  180. .range_max = DA9063_BB_REG_MON_REG_6,
  181. },
  182. };
  183. static const struct regmap_access_table da9063_bb_readable_table = {
  184. .yes_ranges = da9063_bb_readable_ranges,
  185. .n_yes_ranges = ARRAY_SIZE(da9063_bb_readable_ranges),
  186. };
  187. static const struct regmap_access_table da9063_bb_writeable_table = {
  188. .yes_ranges = da9063_bb_writeable_ranges,
  189. .n_yes_ranges = ARRAY_SIZE(da9063_bb_writeable_ranges),
  190. };
  191. static const struct regmap_access_table da9063_bb_volatile_table = {
  192. .yes_ranges = da9063_bb_volatile_ranges,
  193. .n_yes_ranges = ARRAY_SIZE(da9063_bb_volatile_ranges),
  194. };
  195. static const struct regmap_range_cfg da9063_range_cfg[] = {
  196. {
  197. .range_min = DA9063_REG_PAGE_CON,
  198. .range_max = DA9063_REG_CHIP_VARIANT,
  199. .selector_reg = DA9063_REG_PAGE_CON,
  200. .selector_mask = 1 << DA9063_I2C_PAGE_SEL_SHIFT,
  201. .selector_shift = DA9063_I2C_PAGE_SEL_SHIFT,
  202. .window_start = 0,
  203. .window_len = 256,
  204. }
  205. };
  206. static struct regmap_config da9063_regmap_config = {
  207. .reg_bits = 8,
  208. .val_bits = 8,
  209. .ranges = da9063_range_cfg,
  210. .num_ranges = ARRAY_SIZE(da9063_range_cfg),
  211. .max_register = DA9063_REG_CHIP_VARIANT,
  212. .cache_type = REGCACHE_RBTREE,
  213. };
  214. static const struct of_device_id da9063_dt_ids[] = {
  215. { .compatible = "dlg,da9063", },
  216. { }
  217. };
  218. MODULE_DEVICE_TABLE(of, da9063_dt_ids);
  219. static int da9063_i2c_probe(struct i2c_client *i2c,
  220. const struct i2c_device_id *id)
  221. {
  222. struct da9063 *da9063;
  223. int ret;
  224. da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
  225. if (da9063 == NULL)
  226. return -ENOMEM;
  227. i2c_set_clientdata(i2c, da9063);
  228. da9063->dev = &i2c->dev;
  229. da9063->chip_irq = i2c->irq;
  230. if (da9063->variant_code == PMIC_DA9063_AD) {
  231. da9063_regmap_config.rd_table = &da9063_ad_readable_table;
  232. da9063_regmap_config.wr_table = &da9063_ad_writeable_table;
  233. da9063_regmap_config.volatile_table = &da9063_ad_volatile_table;
  234. } else {
  235. da9063_regmap_config.rd_table = &da9063_bb_readable_table;
  236. da9063_regmap_config.wr_table = &da9063_bb_writeable_table;
  237. da9063_regmap_config.volatile_table = &da9063_bb_volatile_table;
  238. }
  239. da9063->regmap = devm_regmap_init_i2c(i2c, &da9063_regmap_config);
  240. if (IS_ERR(da9063->regmap)) {
  241. ret = PTR_ERR(da9063->regmap);
  242. dev_err(da9063->dev, "Failed to allocate register map: %d\n",
  243. ret);
  244. return ret;
  245. }
  246. return da9063_device_init(da9063, i2c->irq);
  247. }
  248. static int da9063_i2c_remove(struct i2c_client *i2c)
  249. {
  250. struct da9063 *da9063 = i2c_get_clientdata(i2c);
  251. da9063_device_exit(da9063);
  252. return 0;
  253. }
  254. static const struct i2c_device_id da9063_i2c_id[] = {
  255. {"da9063", PMIC_DA9063},
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
  259. static struct i2c_driver da9063_i2c_driver = {
  260. .driver = {
  261. .name = "da9063",
  262. .of_match_table = of_match_ptr(da9063_dt_ids),
  263. },
  264. .probe = da9063_i2c_probe,
  265. .remove = da9063_i2c_remove,
  266. .id_table = da9063_i2c_id,
  267. };
  268. module_i2c_driver(da9063_i2c_driver);