fan53555.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
  4. //
  5. // Supported Part Numbers:
  6. // FAN53555UC00X/01X/03X/04X/05X
  7. //
  8. // Copyright (c) 2012 Marvell Technology Ltd.
  9. // Yunfan Zhang <yfzhang@marvell.com>
  10. #include <linux/module.h>
  11. #include <linux/param.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/of_device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/slab.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/fan53555.h>
  22. /* Voltage setting */
  23. #define FAN53555_VSEL0 0x00
  24. #define FAN53555_VSEL1 0x01
  25. /* Control register */
  26. #define FAN53555_CONTROL 0x02
  27. /* IC Type */
  28. #define FAN53555_ID1 0x03
  29. /* IC mask version */
  30. #define FAN53555_ID2 0x04
  31. /* Monitor register */
  32. #define FAN53555_MONITOR 0x05
  33. /* VSEL bit definitions */
  34. #define VSEL_BUCK_EN (1 << 7)
  35. #define VSEL_MODE (1 << 6)
  36. /* Chip ID and Verison */
  37. #define DIE_ID 0x0F /* ID1 */
  38. #define DIE_REV 0x0F /* ID2 */
  39. /* Control bit definitions */
  40. #define CTL_OUTPUT_DISCHG (1 << 7)
  41. #define CTL_SLEW_MASK (0x7 << 4)
  42. #define CTL_SLEW_SHIFT 4
  43. #define CTL_RESET (1 << 2)
  44. #define CTL_MODE_VSEL0_MODE BIT(0)
  45. #define CTL_MODE_VSEL1_MODE BIT(1)
  46. #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
  47. #define FAN53526_NVOLTAGES 128
  48. enum fan53555_vendor {
  49. FAN53526_VENDOR_FAIRCHILD = 0,
  50. FAN53555_VENDOR_FAIRCHILD,
  51. FAN53555_VENDOR_SILERGY,
  52. };
  53. enum {
  54. FAN53526_CHIP_ID_01 = 1,
  55. };
  56. enum {
  57. FAN53526_CHIP_REV_08 = 8,
  58. };
  59. /* IC Type */
  60. enum {
  61. FAN53555_CHIP_ID_00 = 0,
  62. FAN53555_CHIP_ID_01,
  63. FAN53555_CHIP_ID_02,
  64. FAN53555_CHIP_ID_03,
  65. FAN53555_CHIP_ID_04,
  66. FAN53555_CHIP_ID_05,
  67. FAN53555_CHIP_ID_08 = 8,
  68. };
  69. /* IC mask revision */
  70. enum {
  71. FAN53555_CHIP_REV_00 = 0x3,
  72. FAN53555_CHIP_REV_13 = 0xf,
  73. };
  74. enum {
  75. SILERGY_SYR82X = 8,
  76. };
  77. struct fan53555_device_info {
  78. enum fan53555_vendor vendor;
  79. struct device *dev;
  80. struct regulator_desc desc;
  81. struct regulator_init_data *regulator;
  82. /* IC Type and Rev */
  83. int chip_id;
  84. int chip_rev;
  85. /* Voltage setting register */
  86. unsigned int vol_reg;
  87. unsigned int sleep_reg;
  88. /* Voltage range and step(linear) */
  89. unsigned int vsel_min;
  90. unsigned int vsel_step;
  91. unsigned int vsel_count;
  92. /* Mode */
  93. unsigned int mode_reg;
  94. unsigned int mode_mask;
  95. /* Sleep voltage cache */
  96. unsigned int sleep_vol_cache;
  97. };
  98. static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  99. {
  100. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  101. int ret;
  102. if (di->sleep_vol_cache == uV)
  103. return 0;
  104. ret = regulator_map_voltage_linear(rdev, uV, uV);
  105. if (ret < 0)
  106. return ret;
  107. ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
  108. di->desc.vsel_mask, ret);
  109. if (ret < 0)
  110. return ret;
  111. /* Cache the sleep voltage setting.
  112. * Might not be the real voltage which is rounded */
  113. di->sleep_vol_cache = uV;
  114. return 0;
  115. }
  116. static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
  117. {
  118. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  119. return regmap_update_bits(rdev->regmap, di->sleep_reg,
  120. VSEL_BUCK_EN, VSEL_BUCK_EN);
  121. }
  122. static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
  123. {
  124. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  125. return regmap_update_bits(rdev->regmap, di->sleep_reg,
  126. VSEL_BUCK_EN, 0);
  127. }
  128. static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
  129. {
  130. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  131. switch (mode) {
  132. case REGULATOR_MODE_FAST:
  133. regmap_update_bits(rdev->regmap, di->mode_reg,
  134. di->mode_mask, di->mode_mask);
  135. break;
  136. case REGULATOR_MODE_NORMAL:
  137. regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
  145. {
  146. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  147. unsigned int val;
  148. int ret = 0;
  149. ret = regmap_read(rdev->regmap, di->mode_reg, &val);
  150. if (ret < 0)
  151. return ret;
  152. if (val & di->mode_mask)
  153. return REGULATOR_MODE_FAST;
  154. else
  155. return REGULATOR_MODE_NORMAL;
  156. }
  157. static const int slew_rates[] = {
  158. 64000,
  159. 32000,
  160. 16000,
  161. 8000,
  162. 4000,
  163. 2000,
  164. 1000,
  165. 500,
  166. };
  167. static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
  168. {
  169. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  170. int regval = -1, i;
  171. for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
  172. if (ramp <= slew_rates[i])
  173. regval = i;
  174. else
  175. break;
  176. }
  177. if (regval < 0) {
  178. dev_err(di->dev, "unsupported ramp value %d\n", ramp);
  179. return -EINVAL;
  180. }
  181. return regmap_update_bits(rdev->regmap, FAN53555_CONTROL,
  182. CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
  183. }
  184. static const struct regulator_ops fan53555_regulator_ops = {
  185. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  186. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  187. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  188. .map_voltage = regulator_map_voltage_linear,
  189. .list_voltage = regulator_list_voltage_linear,
  190. .set_suspend_voltage = fan53555_set_suspend_voltage,
  191. .enable = regulator_enable_regmap,
  192. .disable = regulator_disable_regmap,
  193. .is_enabled = regulator_is_enabled_regmap,
  194. .set_mode = fan53555_set_mode,
  195. .get_mode = fan53555_get_mode,
  196. .set_ramp_delay = fan53555_set_ramp,
  197. .set_suspend_enable = fan53555_set_suspend_enable,
  198. .set_suspend_disable = fan53555_set_suspend_disable,
  199. };
  200. static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
  201. {
  202. /* Init voltage range and step */
  203. switch (di->chip_id) {
  204. case FAN53526_CHIP_ID_01:
  205. switch (di->chip_rev) {
  206. case FAN53526_CHIP_REV_08:
  207. di->vsel_min = 600000;
  208. di->vsel_step = 6250;
  209. break;
  210. default:
  211. dev_err(di->dev,
  212. "Chip ID %d with rev %d not supported!\n",
  213. di->chip_id, di->chip_rev);
  214. return -EINVAL;
  215. }
  216. break;
  217. default:
  218. dev_err(di->dev,
  219. "Chip ID %d not supported!\n", di->chip_id);
  220. return -EINVAL;
  221. }
  222. di->vsel_count = FAN53526_NVOLTAGES;
  223. return 0;
  224. }
  225. static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
  226. {
  227. /* Init voltage range and step */
  228. switch (di->chip_id) {
  229. case FAN53555_CHIP_ID_00:
  230. switch (di->chip_rev) {
  231. case FAN53555_CHIP_REV_00:
  232. di->vsel_min = 600000;
  233. di->vsel_step = 10000;
  234. break;
  235. case FAN53555_CHIP_REV_13:
  236. di->vsel_min = 800000;
  237. di->vsel_step = 10000;
  238. break;
  239. default:
  240. dev_err(di->dev,
  241. "Chip ID %d with rev %d not supported!\n",
  242. di->chip_id, di->chip_rev);
  243. return -EINVAL;
  244. }
  245. break;
  246. case FAN53555_CHIP_ID_01:
  247. case FAN53555_CHIP_ID_03:
  248. case FAN53555_CHIP_ID_05:
  249. case FAN53555_CHIP_ID_08:
  250. di->vsel_min = 600000;
  251. di->vsel_step = 10000;
  252. break;
  253. case FAN53555_CHIP_ID_04:
  254. di->vsel_min = 603000;
  255. di->vsel_step = 12826;
  256. break;
  257. default:
  258. dev_err(di->dev,
  259. "Chip ID %d not supported!\n", di->chip_id);
  260. return -EINVAL;
  261. }
  262. di->vsel_count = FAN53555_NVOLTAGES;
  263. return 0;
  264. }
  265. static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
  266. {
  267. /* Init voltage range and step */
  268. switch (di->chip_id) {
  269. case SILERGY_SYR82X:
  270. di->vsel_min = 712500;
  271. di->vsel_step = 12500;
  272. break;
  273. default:
  274. dev_err(di->dev,
  275. "Chip ID %d not supported!\n", di->chip_id);
  276. return -EINVAL;
  277. }
  278. di->vsel_count = FAN53555_NVOLTAGES;
  279. return 0;
  280. }
  281. /* For 00,01,03,05 options:
  282. * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
  283. * For 04 option:
  284. * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
  285. * */
  286. static int fan53555_device_setup(struct fan53555_device_info *di,
  287. struct fan53555_platform_data *pdata)
  288. {
  289. int ret = 0;
  290. /* Setup voltage control register */
  291. switch (pdata->sleep_vsel_id) {
  292. case FAN53555_VSEL_ID_0:
  293. di->sleep_reg = FAN53555_VSEL0;
  294. di->vol_reg = FAN53555_VSEL1;
  295. break;
  296. case FAN53555_VSEL_ID_1:
  297. di->sleep_reg = FAN53555_VSEL1;
  298. di->vol_reg = FAN53555_VSEL0;
  299. break;
  300. default:
  301. dev_err(di->dev, "Invalid VSEL ID!\n");
  302. return -EINVAL;
  303. }
  304. /* Setup mode control register */
  305. switch (di->vendor) {
  306. case FAN53526_VENDOR_FAIRCHILD:
  307. di->mode_reg = FAN53555_CONTROL;
  308. switch (pdata->sleep_vsel_id) {
  309. case FAN53555_VSEL_ID_0:
  310. di->mode_mask = CTL_MODE_VSEL1_MODE;
  311. break;
  312. case FAN53555_VSEL_ID_1:
  313. di->mode_mask = CTL_MODE_VSEL0_MODE;
  314. break;
  315. }
  316. break;
  317. case FAN53555_VENDOR_FAIRCHILD:
  318. case FAN53555_VENDOR_SILERGY:
  319. di->mode_reg = di->vol_reg;
  320. di->mode_mask = VSEL_MODE;
  321. break;
  322. default:
  323. dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
  324. return -EINVAL;
  325. }
  326. /* Setup voltage range */
  327. switch (di->vendor) {
  328. case FAN53526_VENDOR_FAIRCHILD:
  329. ret = fan53526_voltages_setup_fairchild(di);
  330. break;
  331. case FAN53555_VENDOR_FAIRCHILD:
  332. ret = fan53555_voltages_setup_fairchild(di);
  333. break;
  334. case FAN53555_VENDOR_SILERGY:
  335. ret = fan53555_voltages_setup_silergy(di);
  336. break;
  337. default:
  338. dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
  339. return -EINVAL;
  340. }
  341. return ret;
  342. }
  343. static int fan53555_regulator_register(struct fan53555_device_info *di,
  344. struct regulator_config *config)
  345. {
  346. struct regulator_desc *rdesc = &di->desc;
  347. struct regulator_dev *rdev;
  348. rdesc->name = "fan53555-reg";
  349. rdesc->supply_name = "vin";
  350. rdesc->ops = &fan53555_regulator_ops;
  351. rdesc->type = REGULATOR_VOLTAGE;
  352. rdesc->n_voltages = di->vsel_count;
  353. rdesc->enable_reg = di->vol_reg;
  354. rdesc->enable_mask = VSEL_BUCK_EN;
  355. rdesc->min_uV = di->vsel_min;
  356. rdesc->uV_step = di->vsel_step;
  357. rdesc->vsel_reg = di->vol_reg;
  358. rdesc->vsel_mask = di->vsel_count - 1;
  359. rdesc->owner = THIS_MODULE;
  360. rdev = devm_regulator_register(di->dev, &di->desc, config);
  361. return PTR_ERR_OR_ZERO(rdev);
  362. }
  363. static const struct regmap_config fan53555_regmap_config = {
  364. .reg_bits = 8,
  365. .val_bits = 8,
  366. };
  367. static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
  368. struct device_node *np,
  369. const struct regulator_desc *desc)
  370. {
  371. struct fan53555_platform_data *pdata;
  372. int ret;
  373. u32 tmp;
  374. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  375. if (!pdata)
  376. return NULL;
  377. pdata->regulator = of_get_regulator_init_data(dev, np, desc);
  378. ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
  379. &tmp);
  380. if (!ret)
  381. pdata->sleep_vsel_id = tmp;
  382. return pdata;
  383. }
  384. static const struct of_device_id fan53555_dt_ids[] = {
  385. {
  386. .compatible = "fcs,fan53526",
  387. .data = (void *)FAN53526_VENDOR_FAIRCHILD,
  388. }, {
  389. .compatible = "fcs,fan53555",
  390. .data = (void *)FAN53555_VENDOR_FAIRCHILD
  391. }, {
  392. .compatible = "silergy,syr827",
  393. .data = (void *)FAN53555_VENDOR_SILERGY,
  394. }, {
  395. .compatible = "silergy,syr828",
  396. .data = (void *)FAN53555_VENDOR_SILERGY,
  397. },
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
  401. static int fan53555_regulator_probe(struct i2c_client *client,
  402. const struct i2c_device_id *id)
  403. {
  404. struct device_node *np = client->dev.of_node;
  405. struct fan53555_device_info *di;
  406. struct fan53555_platform_data *pdata;
  407. struct regulator_config config = { };
  408. struct regmap *regmap;
  409. unsigned int val;
  410. int ret;
  411. di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
  412. GFP_KERNEL);
  413. if (!di)
  414. return -ENOMEM;
  415. pdata = dev_get_platdata(&client->dev);
  416. if (!pdata)
  417. pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
  418. if (!pdata || !pdata->regulator) {
  419. dev_err(&client->dev, "Platform data not found!\n");
  420. return -ENODEV;
  421. }
  422. di->regulator = pdata->regulator;
  423. if (client->dev.of_node) {
  424. di->vendor =
  425. (unsigned long)of_device_get_match_data(&client->dev);
  426. } else {
  427. /* if no ramp constraint set, get the pdata ramp_delay */
  428. if (!di->regulator->constraints.ramp_delay) {
  429. if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) {
  430. dev_err(&client->dev, "Invalid slew_rate\n");
  431. return -EINVAL;
  432. }
  433. di->regulator->constraints.ramp_delay
  434. = slew_rates[pdata->slew_rate];
  435. }
  436. di->vendor = id->driver_data;
  437. }
  438. regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
  439. if (IS_ERR(regmap)) {
  440. dev_err(&client->dev, "Failed to allocate regmap!\n");
  441. return PTR_ERR(regmap);
  442. }
  443. di->dev = &client->dev;
  444. i2c_set_clientdata(client, di);
  445. /* Get chip ID */
  446. ret = regmap_read(regmap, FAN53555_ID1, &val);
  447. if (ret < 0) {
  448. dev_err(&client->dev, "Failed to get chip ID!\n");
  449. return ret;
  450. }
  451. di->chip_id = val & DIE_ID;
  452. /* Get chip revision */
  453. ret = regmap_read(regmap, FAN53555_ID2, &val);
  454. if (ret < 0) {
  455. dev_err(&client->dev, "Failed to get chip Rev!\n");
  456. return ret;
  457. }
  458. di->chip_rev = val & DIE_REV;
  459. dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
  460. di->chip_id, di->chip_rev);
  461. /* Device init */
  462. ret = fan53555_device_setup(di, pdata);
  463. if (ret < 0) {
  464. dev_err(&client->dev, "Failed to setup device!\n");
  465. return ret;
  466. }
  467. /* Register regulator */
  468. config.dev = di->dev;
  469. config.init_data = di->regulator;
  470. config.regmap = regmap;
  471. config.driver_data = di;
  472. config.of_node = np;
  473. ret = fan53555_regulator_register(di, &config);
  474. if (ret < 0)
  475. dev_err(&client->dev, "Failed to register regulator!\n");
  476. return ret;
  477. }
  478. static const struct i2c_device_id fan53555_id[] = {
  479. {
  480. .name = "fan53526",
  481. .driver_data = FAN53526_VENDOR_FAIRCHILD
  482. }, {
  483. .name = "fan53555",
  484. .driver_data = FAN53555_VENDOR_FAIRCHILD
  485. }, {
  486. .name = "syr827",
  487. .driver_data = FAN53555_VENDOR_SILERGY
  488. }, {
  489. .name = "syr828",
  490. .driver_data = FAN53555_VENDOR_SILERGY
  491. },
  492. { },
  493. };
  494. MODULE_DEVICE_TABLE(i2c, fan53555_id);
  495. static struct i2c_driver fan53555_regulator_driver = {
  496. .driver = {
  497. .name = "fan53555-regulator",
  498. .of_match_table = of_match_ptr(fan53555_dt_ids),
  499. },
  500. .probe = fan53555_regulator_probe,
  501. .id_table = fan53555_id,
  502. };
  503. module_i2c_driver(fan53555_regulator_driver);
  504. MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
  505. MODULE_DESCRIPTION("FAN53555 regulator driver");
  506. MODULE_LICENSE("GPL v2");