pfuze100-regulator.c 24 KB

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