pfuze100-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/err.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/regulator/of_regulator.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/regulator/pfuze100.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/regmap.h>
  18. #define PFUZE_NUMREGS 128
  19. #define PFUZE100_VOL_OFFSET 0
  20. #define PFUZE100_STANDBY_OFFSET 1
  21. #define PFUZE100_MODE_OFFSET 3
  22. #define PFUZE100_CONF_OFFSET 4
  23. #define PFUZE100_DEVICEID 0x0
  24. #define PFUZE100_REVID 0x3
  25. #define PFUZE100_FABID 0x4
  26. #define PFUZE100_COINVOL 0x1a
  27. #define PFUZE100_SW1ABVOL 0x20
  28. #define PFUZE100_SW1CVOL 0x2e
  29. #define PFUZE100_SW2VOL 0x35
  30. #define PFUZE100_SW3AVOL 0x3c
  31. #define PFUZE100_SW3BVOL 0x43
  32. #define PFUZE100_SW4VOL 0x4a
  33. #define PFUZE100_SWBSTCON1 0x66
  34. #define PFUZE100_VREFDDRCON 0x6a
  35. #define PFUZE100_VSNVSVOL 0x6b
  36. #define PFUZE100_VGEN1VOL 0x6c
  37. #define PFUZE100_VGEN2VOL 0x6d
  38. #define PFUZE100_VGEN3VOL 0x6e
  39. #define PFUZE100_VGEN4VOL 0x6f
  40. #define PFUZE100_VGEN5VOL 0x70
  41. #define PFUZE100_VGEN6VOL 0x71
  42. enum chips { PFUZE100, PFUZE200, PFUZE3000 = 3 };
  43. struct pfuze_regulator {
  44. struct regulator_desc desc;
  45. unsigned char stby_reg;
  46. unsigned char stby_mask;
  47. };
  48. struct pfuze_chip {
  49. int chip_id;
  50. struct regmap *regmap;
  51. struct device *dev;
  52. struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
  53. struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
  54. struct pfuze_regulator *pfuze_regulators;
  55. };
  56. static const int pfuze100_swbst[] = {
  57. 5000000, 5050000, 5100000, 5150000,
  58. };
  59. static const int pfuze100_vsnvs[] = {
  60. 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000,
  61. };
  62. static const int pfuze100_coin[] = {
  63. 2500000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
  64. };
  65. static const int pfuze3000_sw1a[] = {
  66. 700000, 725000, 750000, 775000, 800000, 825000, 850000, 875000,
  67. 900000, 925000, 950000, 975000, 1000000, 1025000, 1050000, 1075000,
  68. 1100000, 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000,
  69. 1300000, 1325000, 1350000, 1375000, 1400000, 1425000, 1800000, 3300000,
  70. };
  71. static const int pfuze3000_sw2lo[] = {
  72. 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000,
  73. };
  74. static const int pfuze3000_sw2hi[] = {
  75. 2500000, 2800000, 2850000, 3000000, 3100000, 3150000, 3200000, 3300000,
  76. };
  77. static const struct i2c_device_id pfuze_device_id[] = {
  78. {.name = "pfuze100", .driver_data = PFUZE100},
  79. {.name = "pfuze200", .driver_data = PFUZE200},
  80. {.name = "pfuze3000", .driver_data = PFUZE3000},
  81. { }
  82. };
  83. MODULE_DEVICE_TABLE(i2c, pfuze_device_id);
  84. static const struct of_device_id pfuze_dt_ids[] = {
  85. { .compatible = "fsl,pfuze100", .data = (void *)PFUZE100},
  86. { .compatible = "fsl,pfuze200", .data = (void *)PFUZE200},
  87. { .compatible = "fsl,pfuze3000", .data = (void *)PFUZE3000},
  88. { }
  89. };
  90. MODULE_DEVICE_TABLE(of, pfuze_dt_ids);
  91. static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  92. {
  93. struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev);
  94. int id = rdev_get_id(rdev);
  95. unsigned int ramp_bits;
  96. int ret;
  97. if (id < PFUZE100_SWBST) {
  98. ramp_delay = 12500 / ramp_delay;
  99. ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
  100. ret = regmap_update_bits(pfuze100->regmap,
  101. rdev->desc->vsel_reg + 4,
  102. 0xc0, ramp_bits << 6);
  103. if (ret < 0)
  104. dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
  105. } else
  106. ret = -EACCES;
  107. return ret;
  108. }
  109. static const struct regulator_ops pfuze100_ldo_regulator_ops = {
  110. .enable = regulator_enable_regmap,
  111. .disable = regulator_disable_regmap,
  112. .is_enabled = regulator_is_enabled_regmap,
  113. .list_voltage = regulator_list_voltage_linear,
  114. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  115. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  116. };
  117. static const struct regulator_ops pfuze100_fixed_regulator_ops = {
  118. .enable = regulator_enable_regmap,
  119. .disable = regulator_disable_regmap,
  120. .is_enabled = regulator_is_enabled_regmap,
  121. .list_voltage = regulator_list_voltage_linear,
  122. };
  123. static const struct regulator_ops pfuze100_sw_regulator_ops = {
  124. .enable = regulator_enable_regmap,
  125. .disable = regulator_disable_regmap,
  126. .is_enabled = regulator_is_enabled_regmap,
  127. .list_voltage = regulator_list_voltage_linear,
  128. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  129. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  130. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  131. .set_ramp_delay = pfuze100_set_ramp_delay,
  132. };
  133. static const struct regulator_ops pfuze100_swb_regulator_ops = {
  134. .enable = regulator_enable_regmap,
  135. .disable = regulator_disable_regmap,
  136. .is_enabled = regulator_is_enabled_regmap,
  137. .list_voltage = regulator_list_voltage_table,
  138. .map_voltage = regulator_map_voltage_ascend,
  139. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  140. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  141. };
  142. #define PFUZE100_FIXED_REG(_chip, _name, base, voltage) \
  143. [_chip ## _ ## _name] = { \
  144. .desc = { \
  145. .name = #_name, \
  146. .n_voltages = 1, \
  147. .ops = &pfuze100_fixed_regulator_ops, \
  148. .type = REGULATOR_VOLTAGE, \
  149. .id = _chip ## _ ## _name, \
  150. .owner = THIS_MODULE, \
  151. .min_uV = (voltage), \
  152. .enable_reg = (base), \
  153. .enable_mask = 0x10, \
  154. }, \
  155. }
  156. #define PFUZE100_SW_REG(_chip, _name, base, min, max, step) \
  157. [_chip ## _ ## _name] = { \
  158. .desc = { \
  159. .name = #_name,\
  160. .n_voltages = ((max) - (min)) / (step) + 1, \
  161. .ops = &pfuze100_sw_regulator_ops, \
  162. .type = REGULATOR_VOLTAGE, \
  163. .id = _chip ## _ ## _name, \
  164. .owner = THIS_MODULE, \
  165. .min_uV = (min), \
  166. .uV_step = (step), \
  167. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  168. .vsel_mask = 0x3f, \
  169. .enable_reg = (base) + PFUZE100_MODE_OFFSET, \
  170. .enable_val = 0xc, \
  171. .disable_val = 0x0, \
  172. .enable_mask = 0xf, \
  173. .enable_time = 500, \
  174. }, \
  175. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  176. .stby_mask = 0x3f, \
  177. }
  178. #define PFUZE100_SWB_REG(_chip, _name, base, mask, voltages) \
  179. [_chip ## _ ## _name] = { \
  180. .desc = { \
  181. .name = #_name, \
  182. .n_voltages = ARRAY_SIZE(voltages), \
  183. .ops = &pfuze100_swb_regulator_ops, \
  184. .type = REGULATOR_VOLTAGE, \
  185. .id = _chip ## _ ## _name, \
  186. .owner = THIS_MODULE, \
  187. .volt_table = voltages, \
  188. .vsel_reg = (base), \
  189. .vsel_mask = (mask), \
  190. .enable_reg = (base), \
  191. .enable_mask = 0x48, \
  192. }, \
  193. }
  194. #define PFUZE100_VGEN_REG(_chip, _name, base, min, max, step) \
  195. [_chip ## _ ## _name] = { \
  196. .desc = { \
  197. .name = #_name, \
  198. .n_voltages = ((max) - (min)) / (step) + 1, \
  199. .ops = &pfuze100_ldo_regulator_ops, \
  200. .type = REGULATOR_VOLTAGE, \
  201. .id = _chip ## _ ## _name, \
  202. .owner = THIS_MODULE, \
  203. .min_uV = (min), \
  204. .uV_step = (step), \
  205. .vsel_reg = (base), \
  206. .vsel_mask = 0xf, \
  207. .enable_reg = (base), \
  208. .enable_mask = 0x10, \
  209. }, \
  210. .stby_reg = (base), \
  211. .stby_mask = 0x20, \
  212. }
  213. #define PFUZE100_COIN_REG(_chip, _name, base, mask, voltages) \
  214. [_chip ## _ ## _name] = { \
  215. .desc = { \
  216. .name = #_name, \
  217. .n_voltages = ARRAY_SIZE(voltages), \
  218. .ops = &pfuze100_swb_regulator_ops, \
  219. .type = REGULATOR_VOLTAGE, \
  220. .id = _chip ## _ ## _name, \
  221. .owner = THIS_MODULE, \
  222. .volt_table = voltages, \
  223. .vsel_reg = (base), \
  224. .vsel_mask = (mask), \
  225. .enable_reg = (base), \
  226. .enable_mask = 0x8, \
  227. }, \
  228. }
  229. #define PFUZE3000_VCC_REG(_chip, _name, base, min, max, step) { \
  230. .desc = { \
  231. .name = #_name, \
  232. .n_voltages = ((max) - (min)) / (step) + 1, \
  233. .ops = &pfuze100_ldo_regulator_ops, \
  234. .type = REGULATOR_VOLTAGE, \
  235. .id = _chip ## _ ## _name, \
  236. .owner = THIS_MODULE, \
  237. .min_uV = (min), \
  238. .uV_step = (step), \
  239. .vsel_reg = (base), \
  240. .vsel_mask = 0x3, \
  241. .enable_reg = (base), \
  242. .enable_mask = 0x10, \
  243. }, \
  244. .stby_reg = (base), \
  245. .stby_mask = 0x20, \
  246. }
  247. #define PFUZE3000_SW2_REG(_chip, _name, base, min, max, step) { \
  248. .desc = { \
  249. .name = #_name,\
  250. .n_voltages = ((max) - (min)) / (step) + 1, \
  251. .ops = &pfuze100_sw_regulator_ops, \
  252. .type = REGULATOR_VOLTAGE, \
  253. .id = _chip ## _ ## _name, \
  254. .owner = THIS_MODULE, \
  255. .min_uV = (min), \
  256. .uV_step = (step), \
  257. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  258. .vsel_mask = 0x7, \
  259. }, \
  260. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  261. .stby_mask = 0x7, \
  262. }
  263. #define PFUZE3000_SW3_REG(_chip, _name, base, min, max, step) { \
  264. .desc = { \
  265. .name = #_name,\
  266. .n_voltages = ((max) - (min)) / (step) + 1, \
  267. .ops = &pfuze100_sw_regulator_ops, \
  268. .type = REGULATOR_VOLTAGE, \
  269. .id = _chip ## _ ## _name, \
  270. .owner = THIS_MODULE, \
  271. .min_uV = (min), \
  272. .uV_step = (step), \
  273. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  274. .vsel_mask = 0xf, \
  275. }, \
  276. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  277. .stby_mask = 0xf, \
  278. }
  279. /* PFUZE100 */
  280. static struct pfuze_regulator pfuze100_regulators[] = {
  281. PFUZE100_SW_REG(PFUZE100, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  282. PFUZE100_SW_REG(PFUZE100, SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  283. PFUZE100_SW_REG(PFUZE100, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  284. PFUZE100_SW_REG(PFUZE100, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  285. PFUZE100_SW_REG(PFUZE100, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  286. PFUZE100_SW_REG(PFUZE100, SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  287. PFUZE100_SWB_REG(PFUZE100, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  288. PFUZE100_SWB_REG(PFUZE100, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  289. PFUZE100_FIXED_REG(PFUZE100, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  290. PFUZE100_VGEN_REG(PFUZE100, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  291. PFUZE100_VGEN_REG(PFUZE100, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  292. PFUZE100_VGEN_REG(PFUZE100, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  293. PFUZE100_VGEN_REG(PFUZE100, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  294. PFUZE100_VGEN_REG(PFUZE100, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  295. PFUZE100_VGEN_REG(PFUZE100, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  296. };
  297. static struct pfuze_regulator pfuze200_regulators[] = {
  298. PFUZE100_SW_REG(PFUZE200, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  299. PFUZE100_SW_REG(PFUZE200, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  300. PFUZE100_SW_REG(PFUZE200, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  301. PFUZE100_SW_REG(PFUZE200, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  302. PFUZE100_SWB_REG(PFUZE200, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  303. PFUZE100_SWB_REG(PFUZE200, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  304. PFUZE100_FIXED_REG(PFUZE200, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  305. PFUZE100_VGEN_REG(PFUZE200, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  306. PFUZE100_VGEN_REG(PFUZE200, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  307. PFUZE100_VGEN_REG(PFUZE200, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  308. PFUZE100_VGEN_REG(PFUZE200, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  309. PFUZE100_VGEN_REG(PFUZE200, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  310. PFUZE100_VGEN_REG(PFUZE200, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  311. PFUZE100_COIN_REG(PFUZE200, COIN, PFUZE100_COINVOL, 0x7, pfuze100_coin),
  312. };
  313. static struct pfuze_regulator pfuze3000_regulators[] = {
  314. PFUZE100_SWB_REG(PFUZE3000, SW1A, PFUZE100_SW1ABVOL, 0x1f, pfuze3000_sw1a),
  315. PFUZE100_SW_REG(PFUZE3000, SW1B, PFUZE100_SW1CVOL, 700000, 1475000, 25000),
  316. PFUZE100_SWB_REG(PFUZE3000, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  317. PFUZE3000_SW3_REG(PFUZE3000, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  318. PFUZE100_SWB_REG(PFUZE3000, SWBST, PFUZE100_SWBSTCON1, 0x3, pfuze100_swbst),
  319. PFUZE100_SWB_REG(PFUZE3000, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  320. PFUZE100_FIXED_REG(PFUZE3000, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  321. PFUZE100_VGEN_REG(PFUZE3000, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  322. PFUZE100_VGEN_REG(PFUZE3000, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  323. PFUZE3000_VCC_REG(PFUZE3000, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  324. PFUZE3000_VCC_REG(PFUZE3000, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  325. PFUZE100_VGEN_REG(PFUZE3000, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  326. PFUZE100_VGEN_REG(PFUZE3000, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  327. };
  328. #ifdef CONFIG_OF
  329. /* PFUZE100 */
  330. static struct of_regulator_match pfuze100_matches[] = {
  331. { .name = "sw1ab", },
  332. { .name = "sw1c", },
  333. { .name = "sw2", },
  334. { .name = "sw3a", },
  335. { .name = "sw3b", },
  336. { .name = "sw4", },
  337. { .name = "swbst", },
  338. { .name = "vsnvs", },
  339. { .name = "vrefddr", },
  340. { .name = "vgen1", },
  341. { .name = "vgen2", },
  342. { .name = "vgen3", },
  343. { .name = "vgen4", },
  344. { .name = "vgen5", },
  345. { .name = "vgen6", },
  346. };
  347. /* PFUZE200 */
  348. static struct of_regulator_match pfuze200_matches[] = {
  349. { .name = "sw1ab", },
  350. { .name = "sw2", },
  351. { .name = "sw3a", },
  352. { .name = "sw3b", },
  353. { .name = "swbst", },
  354. { .name = "vsnvs", },
  355. { .name = "vrefddr", },
  356. { .name = "vgen1", },
  357. { .name = "vgen2", },
  358. { .name = "vgen3", },
  359. { .name = "vgen4", },
  360. { .name = "vgen5", },
  361. { .name = "vgen6", },
  362. { .name = "coin", },
  363. };
  364. /* PFUZE3000 */
  365. static struct of_regulator_match pfuze3000_matches[] = {
  366. { .name = "sw1a", },
  367. { .name = "sw1b", },
  368. { .name = "sw2", },
  369. { .name = "sw3", },
  370. { .name = "swbst", },
  371. { .name = "vsnvs", },
  372. { .name = "vrefddr", },
  373. { .name = "vldo1", },
  374. { .name = "vldo2", },
  375. { .name = "vccsd", },
  376. { .name = "v33", },
  377. { .name = "vldo3", },
  378. { .name = "vldo4", },
  379. };
  380. static struct of_regulator_match *pfuze_matches;
  381. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  382. {
  383. struct device *dev = chip->dev;
  384. struct device_node *np, *parent;
  385. int ret;
  386. np = of_node_get(dev->of_node);
  387. if (!np)
  388. return -EINVAL;
  389. parent = of_get_child_by_name(np, "regulators");
  390. if (!parent) {
  391. dev_err(dev, "regulators node not found\n");
  392. return -EINVAL;
  393. }
  394. switch (chip->chip_id) {
  395. case PFUZE3000:
  396. pfuze_matches = pfuze3000_matches;
  397. ret = of_regulator_match(dev, parent, pfuze3000_matches,
  398. ARRAY_SIZE(pfuze3000_matches));
  399. break;
  400. case PFUZE200:
  401. pfuze_matches = pfuze200_matches;
  402. ret = of_regulator_match(dev, parent, pfuze200_matches,
  403. ARRAY_SIZE(pfuze200_matches));
  404. break;
  405. case PFUZE100:
  406. default:
  407. pfuze_matches = pfuze100_matches;
  408. ret = of_regulator_match(dev, parent, pfuze100_matches,
  409. ARRAY_SIZE(pfuze100_matches));
  410. break;
  411. }
  412. of_node_put(parent);
  413. if (ret < 0) {
  414. dev_err(dev, "Error parsing regulator init data: %d\n",
  415. ret);
  416. return ret;
  417. }
  418. return 0;
  419. }
  420. static inline struct regulator_init_data *match_init_data(int index)
  421. {
  422. return pfuze_matches[index].init_data;
  423. }
  424. static inline struct device_node *match_of_node(int index)
  425. {
  426. return pfuze_matches[index].of_node;
  427. }
  428. #else
  429. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  430. {
  431. return 0;
  432. }
  433. static inline struct regulator_init_data *match_init_data(int index)
  434. {
  435. return NULL;
  436. }
  437. static inline struct device_node *match_of_node(int index)
  438. {
  439. return NULL;
  440. }
  441. #endif
  442. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  443. {
  444. unsigned int value;
  445. int ret;
  446. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  447. if (ret)
  448. return ret;
  449. if (((value & 0x0f) == 0x8) && (pfuze_chip->chip_id == PFUZE100)) {
  450. /*
  451. * Freescale misprogrammed 1-3% of parts prior to week 8 of 2013
  452. * as ID=8 in PFUZE100
  453. */
  454. dev_info(pfuze_chip->dev, "Assuming misprogrammed ID=0x8");
  455. } else if ((value & 0x0f) != pfuze_chip->chip_id &&
  456. (value & 0xf0) >> 4 != pfuze_chip->chip_id) {
  457. /* device id NOT match with your setting */
  458. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  459. return -ENODEV;
  460. }
  461. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  462. if (ret)
  463. return ret;
  464. dev_info(pfuze_chip->dev,
  465. "Full layer: %x, Metal layer: %x\n",
  466. (value & 0xf0) >> 4, value & 0x0f);
  467. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  468. if (ret)
  469. return ret;
  470. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  471. (value & 0xc) >> 2, value & 0x3);
  472. return 0;
  473. }
  474. static const struct regmap_config pfuze_regmap_config = {
  475. .reg_bits = 8,
  476. .val_bits = 8,
  477. .max_register = PFUZE_NUMREGS - 1,
  478. .cache_type = REGCACHE_RBTREE,
  479. };
  480. static int pfuze100_regulator_probe(struct i2c_client *client,
  481. const struct i2c_device_id *id)
  482. {
  483. struct pfuze_chip *pfuze_chip;
  484. struct pfuze_regulator_platform_data *pdata =
  485. dev_get_platdata(&client->dev);
  486. struct regulator_config config = { };
  487. int i, ret;
  488. const struct of_device_id *match;
  489. u32 regulator_num;
  490. u32 sw_check_start, sw_check_end, sw_hi = 0x40;
  491. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  492. GFP_KERNEL);
  493. if (!pfuze_chip)
  494. return -ENOMEM;
  495. if (client->dev.of_node) {
  496. match = of_match_device(of_match_ptr(pfuze_dt_ids),
  497. &client->dev);
  498. if (!match) {
  499. dev_err(&client->dev, "Error: No device match found\n");
  500. return -ENODEV;
  501. }
  502. pfuze_chip->chip_id = (int)(long)match->data;
  503. } else if (id) {
  504. pfuze_chip->chip_id = id->driver_data;
  505. } else {
  506. dev_err(&client->dev, "No dts match or id table match found\n");
  507. return -ENODEV;
  508. }
  509. i2c_set_clientdata(client, pfuze_chip);
  510. pfuze_chip->dev = &client->dev;
  511. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  512. if (IS_ERR(pfuze_chip->regmap)) {
  513. ret = PTR_ERR(pfuze_chip->regmap);
  514. dev_err(&client->dev,
  515. "regmap allocation failed with err %d\n", ret);
  516. return ret;
  517. }
  518. ret = pfuze_identify(pfuze_chip);
  519. if (ret) {
  520. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  521. return ret;
  522. }
  523. /* use the right regulators after identify the right device */
  524. switch (pfuze_chip->chip_id) {
  525. case PFUZE3000:
  526. pfuze_chip->pfuze_regulators = pfuze3000_regulators;
  527. regulator_num = ARRAY_SIZE(pfuze3000_regulators);
  528. sw_check_start = PFUZE3000_SW2;
  529. sw_check_end = PFUZE3000_SW2;
  530. sw_hi = 1 << 3;
  531. break;
  532. case PFUZE200:
  533. pfuze_chip->pfuze_regulators = pfuze200_regulators;
  534. regulator_num = ARRAY_SIZE(pfuze200_regulators);
  535. sw_check_start = PFUZE200_SW2;
  536. sw_check_end = PFUZE200_SW3B;
  537. break;
  538. case PFUZE100:
  539. default:
  540. pfuze_chip->pfuze_regulators = pfuze100_regulators;
  541. regulator_num = ARRAY_SIZE(pfuze100_regulators);
  542. sw_check_start = PFUZE100_SW2;
  543. sw_check_end = PFUZE100_SW4;
  544. break;
  545. }
  546. dev_info(&client->dev, "pfuze%s found.\n",
  547. (pfuze_chip->chip_id == PFUZE100) ? "100" :
  548. ((pfuze_chip->chip_id == PFUZE200) ? "200" : "3000"));
  549. memcpy(pfuze_chip->regulator_descs, pfuze_chip->pfuze_regulators,
  550. sizeof(pfuze_chip->regulator_descs));
  551. ret = pfuze_parse_regulators_dt(pfuze_chip);
  552. if (ret)
  553. return ret;
  554. for (i = 0; i < regulator_num; i++) {
  555. struct regulator_init_data *init_data;
  556. struct regulator_desc *desc;
  557. int val;
  558. desc = &pfuze_chip->regulator_descs[i].desc;
  559. if (pdata)
  560. init_data = pdata->init_data[i];
  561. else
  562. init_data = match_init_data(i);
  563. /* SW2~SW4 high bit check and modify the voltage value table */
  564. if (i >= sw_check_start && i <= sw_check_end) {
  565. regmap_read(pfuze_chip->regmap, desc->vsel_reg, &val);
  566. if (val & sw_hi) {
  567. if (pfuze_chip->chip_id == PFUZE3000) {
  568. desc->volt_table = pfuze3000_sw2hi;
  569. desc->n_voltages = ARRAY_SIZE(pfuze3000_sw2hi);
  570. } else {
  571. desc->min_uV = 800000;
  572. desc->uV_step = 50000;
  573. desc->n_voltages = 51;
  574. }
  575. }
  576. }
  577. config.dev = &client->dev;
  578. config.init_data = init_data;
  579. config.driver_data = pfuze_chip;
  580. config.of_node = match_of_node(i);
  581. pfuze_chip->regulators[i] =
  582. devm_regulator_register(&client->dev, desc, &config);
  583. if (IS_ERR(pfuze_chip->regulators[i])) {
  584. dev_err(&client->dev, "register regulator%s failed\n",
  585. pfuze_chip->pfuze_regulators[i].desc.name);
  586. return PTR_ERR(pfuze_chip->regulators[i]);
  587. }
  588. }
  589. return 0;
  590. }
  591. static struct i2c_driver pfuze_driver = {
  592. .id_table = pfuze_device_id,
  593. .driver = {
  594. .name = "pfuze100-regulator",
  595. .of_match_table = pfuze_dt_ids,
  596. },
  597. .probe = pfuze100_regulator_probe,
  598. };
  599. module_i2c_driver(pfuze_driver);
  600. MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
  601. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100/200/3000 PMIC");
  602. MODULE_LICENSE("GPL v2");