max8973-regulator.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * max8973-regulator.c -- Maxim max8973
  3. *
  4. * Regulator driver for MAXIM 8973 DC-DC step-down switching regulator.
  5. *
  6. * Copyright (c) 2012, NVIDIA Corporation.
  7. *
  8. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  15. * whether express or implied; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307, USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/err.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/regulator/driver.h>
  32. #include <linux/regulator/machine.h>
  33. #include <linux/regulator/max8973-regulator.h>
  34. #include <linux/regulator/of_regulator.h>
  35. #include <linux/gpio.h>
  36. #include <linux/of_gpio.h>
  37. #include <linux/i2c.h>
  38. #include <linux/slab.h>
  39. #include <linux/regmap.h>
  40. #include <linux/thermal.h>
  41. #include <linux/irq.h>
  42. #include <linux/interrupt.h>
  43. /* Register definitions */
  44. #define MAX8973_VOUT 0x0
  45. #define MAX8973_VOUT_DVS 0x1
  46. #define MAX8973_CONTROL1 0x2
  47. #define MAX8973_CONTROL2 0x3
  48. #define MAX8973_CHIPID1 0x4
  49. #define MAX8973_CHIPID2 0x5
  50. #define MAX8973_MAX_VOUT_REG 2
  51. /* MAX8973_VOUT */
  52. #define MAX8973_VOUT_ENABLE BIT(7)
  53. #define MAX8973_VOUT_MASK 0x7F
  54. /* MAX8973_VOUT_DVS */
  55. #define MAX8973_DVS_VOUT_MASK 0x7F
  56. /* MAX8973_CONTROL1 */
  57. #define MAX8973_SNS_ENABLE BIT(7)
  58. #define MAX8973_FPWM_EN_M BIT(6)
  59. #define MAX8973_NFSR_ENABLE BIT(5)
  60. #define MAX8973_AD_ENABLE BIT(4)
  61. #define MAX8973_BIAS_ENABLE BIT(3)
  62. #define MAX8973_FREQSHIFT_9PER BIT(2)
  63. #define MAX8973_RAMP_12mV_PER_US 0x0
  64. #define MAX8973_RAMP_25mV_PER_US 0x1
  65. #define MAX8973_RAMP_50mV_PER_US 0x2
  66. #define MAX8973_RAMP_200mV_PER_US 0x3
  67. #define MAX8973_RAMP_MASK 0x3
  68. /* MAX8973_CONTROL2 */
  69. #define MAX8973_WDTMR_ENABLE BIT(6)
  70. #define MAX8973_DISCH_ENBABLE BIT(5)
  71. #define MAX8973_FT_ENABLE BIT(4)
  72. #define MAX77621_T_JUNCTION_120 BIT(7)
  73. #define MAX8973_CKKADV_TRIP_MASK 0xC
  74. #define MAX8973_CKKADV_TRIP_DISABLE 0xC
  75. #define MAX8973_CKKADV_TRIP_75mV_PER_US 0x0
  76. #define MAX8973_CKKADV_TRIP_150mV_PER_US 0x4
  77. #define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS 0x8
  78. #define MAX8973_CONTROL_CLKADV_TRIP_MASK 0x00030000
  79. #define MAX8973_INDUCTOR_MIN_30_PER 0x0
  80. #define MAX8973_INDUCTOR_NOMINAL 0x1
  81. #define MAX8973_INDUCTOR_PLUS_30_PER 0x2
  82. #define MAX8973_INDUCTOR_PLUS_60_PER 0x3
  83. #define MAX8973_CONTROL_INDUCTOR_VALUE_MASK 0x00300000
  84. #define MAX8973_MIN_VOLATGE 606250
  85. #define MAX8973_MAX_VOLATGE 1400000
  86. #define MAX8973_VOLATGE_STEP 6250
  87. #define MAX8973_BUCK_N_VOLTAGE 0x80
  88. #define MAX77621_CHIPID_TJINT_S BIT(0)
  89. #define MAX77621_NORMAL_OPERATING_TEMP 100000
  90. #define MAX77621_TJINT_WARNING_TEMP_120 120000
  91. #define MAX77621_TJINT_WARNING_TEMP_140 140000
  92. enum device_id {
  93. MAX8973,
  94. MAX77621
  95. };
  96. /* Maxim 8973 chip information */
  97. struct max8973_chip {
  98. struct device *dev;
  99. struct regulator_desc desc;
  100. struct regmap *regmap;
  101. bool enable_external_control;
  102. int enable_gpio;
  103. int dvs_gpio;
  104. int lru_index[MAX8973_MAX_VOUT_REG];
  105. int curr_vout_val[MAX8973_MAX_VOUT_REG];
  106. int curr_vout_reg;
  107. int curr_gpio_val;
  108. struct regulator_ops ops;
  109. enum device_id id;
  110. int junction_temp_warning;
  111. int irq;
  112. struct thermal_zone_device *tz_device;
  113. };
  114. /*
  115. * find_voltage_set_register: Find new voltage configuration register (VOUT).
  116. * The finding of the new VOUT register will be based on the LRU mechanism.
  117. * Each VOUT register will have different voltage configured . This
  118. * Function will look if any of the VOUT register have requested voltage set
  119. * or not.
  120. * - If it is already there then it will make that register as most
  121. * recently used and return as found so that caller need not to set
  122. * the VOUT register but need to set the proper gpios to select this
  123. * VOUT register.
  124. * - If requested voltage is not found then it will use the least
  125. * recently mechanism to get new VOUT register for new configuration
  126. * and will return not_found so that caller need to set new VOUT
  127. * register and then gpios (both).
  128. */
  129. static bool find_voltage_set_register(struct max8973_chip *tps,
  130. int req_vsel, int *vout_reg, int *gpio_val)
  131. {
  132. int i;
  133. bool found = false;
  134. int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
  135. int found_index = MAX8973_MAX_VOUT_REG - 1;
  136. for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
  137. if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
  138. new_vout_reg = tps->lru_index[i];
  139. found_index = i;
  140. found = true;
  141. goto update_lru_index;
  142. }
  143. }
  144. update_lru_index:
  145. for (i = found_index; i > 0; i--)
  146. tps->lru_index[i] = tps->lru_index[i - 1];
  147. tps->lru_index[0] = new_vout_reg;
  148. *gpio_val = new_vout_reg;
  149. *vout_reg = MAX8973_VOUT + new_vout_reg;
  150. return found;
  151. }
  152. static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
  153. {
  154. struct max8973_chip *max = rdev_get_drvdata(rdev);
  155. unsigned int data;
  156. int ret;
  157. ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
  158. if (ret < 0) {
  159. dev_err(max->dev, "register %d read failed, err = %d\n",
  160. max->curr_vout_reg, ret);
  161. return ret;
  162. }
  163. return data & MAX8973_VOUT_MASK;
  164. }
  165. static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
  166. unsigned vsel)
  167. {
  168. struct max8973_chip *max = rdev_get_drvdata(rdev);
  169. int ret;
  170. bool found = false;
  171. int vout_reg = max->curr_vout_reg;
  172. int gpio_val = max->curr_gpio_val;
  173. /*
  174. * If gpios are available to select the VOUT register then least
  175. * recently used register for new configuration.
  176. */
  177. if (gpio_is_valid(max->dvs_gpio))
  178. found = find_voltage_set_register(max, vsel,
  179. &vout_reg, &gpio_val);
  180. if (!found) {
  181. ret = regmap_update_bits(max->regmap, vout_reg,
  182. MAX8973_VOUT_MASK, vsel);
  183. if (ret < 0) {
  184. dev_err(max->dev, "register %d update failed, err %d\n",
  185. vout_reg, ret);
  186. return ret;
  187. }
  188. max->curr_vout_reg = vout_reg;
  189. max->curr_vout_val[gpio_val] = vsel;
  190. }
  191. /* Select proper VOUT register vio gpios */
  192. if (gpio_is_valid(max->dvs_gpio)) {
  193. gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
  194. max->curr_gpio_val = gpio_val;
  195. }
  196. return 0;
  197. }
  198. static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
  199. {
  200. struct max8973_chip *max = rdev_get_drvdata(rdev);
  201. int ret;
  202. int pwm;
  203. /* Enable force PWM mode in FAST mode only. */
  204. switch (mode) {
  205. case REGULATOR_MODE_FAST:
  206. pwm = MAX8973_FPWM_EN_M;
  207. break;
  208. case REGULATOR_MODE_NORMAL:
  209. pwm = 0;
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
  215. MAX8973_FPWM_EN_M, pwm);
  216. if (ret < 0)
  217. dev_err(max->dev, "register %d update failed, err %d\n",
  218. MAX8973_CONTROL1, ret);
  219. return ret;
  220. }
  221. static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
  222. {
  223. struct max8973_chip *max = rdev_get_drvdata(rdev);
  224. unsigned int data;
  225. int ret;
  226. ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
  227. if (ret < 0) {
  228. dev_err(max->dev, "register %d read failed, err %d\n",
  229. MAX8973_CONTROL1, ret);
  230. return ret;
  231. }
  232. return (data & MAX8973_FPWM_EN_M) ?
  233. REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
  234. }
  235. static int max8973_set_ramp_delay(struct regulator_dev *rdev,
  236. int ramp_delay)
  237. {
  238. struct max8973_chip *max = rdev_get_drvdata(rdev);
  239. unsigned int control;
  240. int ret;
  241. /* Set ramp delay */
  242. if (ramp_delay <= 12000)
  243. control = MAX8973_RAMP_12mV_PER_US;
  244. else if (ramp_delay <= 25000)
  245. control = MAX8973_RAMP_25mV_PER_US;
  246. else if (ramp_delay <= 50000)
  247. control = MAX8973_RAMP_50mV_PER_US;
  248. else if (ramp_delay <= 200000)
  249. control = MAX8973_RAMP_200mV_PER_US;
  250. else
  251. return -EINVAL;
  252. ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
  253. MAX8973_RAMP_MASK, control);
  254. if (ret < 0)
  255. dev_err(max->dev, "register %d update failed, %d",
  256. MAX8973_CONTROL1, ret);
  257. return ret;
  258. }
  259. static int max8973_set_current_limit(struct regulator_dev *rdev,
  260. int min_ua, int max_ua)
  261. {
  262. struct max8973_chip *max = rdev_get_drvdata(rdev);
  263. unsigned int val;
  264. int ret;
  265. if (max_ua <= 9000000)
  266. val = MAX8973_CKKADV_TRIP_75mV_PER_US;
  267. else if (max_ua <= 12000000)
  268. val = MAX8973_CKKADV_TRIP_150mV_PER_US;
  269. else
  270. val = MAX8973_CKKADV_TRIP_DISABLE;
  271. ret = regmap_update_bits(max->regmap, MAX8973_CONTROL2,
  272. MAX8973_CKKADV_TRIP_MASK, val);
  273. if (ret < 0) {
  274. dev_err(max->dev, "register %d update failed: %d\n",
  275. MAX8973_CONTROL2, ret);
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. static int max8973_get_current_limit(struct regulator_dev *rdev)
  281. {
  282. struct max8973_chip *max = rdev_get_drvdata(rdev);
  283. unsigned int control2;
  284. int ret;
  285. ret = regmap_read(max->regmap, MAX8973_CONTROL2, &control2);
  286. if (ret < 0) {
  287. dev_err(max->dev, "register %d read failed: %d\n",
  288. MAX8973_CONTROL2, ret);
  289. return ret;
  290. }
  291. switch (control2 & MAX8973_CKKADV_TRIP_MASK) {
  292. case MAX8973_CKKADV_TRIP_DISABLE:
  293. return 15000000;
  294. case MAX8973_CKKADV_TRIP_150mV_PER_US:
  295. return 12000000;
  296. case MAX8973_CKKADV_TRIP_75mV_PER_US:
  297. return 9000000;
  298. default:
  299. break;
  300. }
  301. return 9000000;
  302. }
  303. static const struct regulator_ops max8973_dcdc_ops = {
  304. .get_voltage_sel = max8973_dcdc_get_voltage_sel,
  305. .set_voltage_sel = max8973_dcdc_set_voltage_sel,
  306. .list_voltage = regulator_list_voltage_linear,
  307. .set_mode = max8973_dcdc_set_mode,
  308. .get_mode = max8973_dcdc_get_mode,
  309. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  310. .set_ramp_delay = max8973_set_ramp_delay,
  311. };
  312. static int max8973_init_dcdc(struct max8973_chip *max,
  313. struct max8973_regulator_platform_data *pdata)
  314. {
  315. int ret;
  316. uint8_t control1 = 0;
  317. uint8_t control2 = 0;
  318. unsigned int data;
  319. ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
  320. if (ret < 0) {
  321. dev_err(max->dev, "register %d read failed, err = %d",
  322. MAX8973_CONTROL1, ret);
  323. return ret;
  324. }
  325. control1 = data & MAX8973_RAMP_MASK;
  326. switch (control1) {
  327. case MAX8973_RAMP_12mV_PER_US:
  328. max->desc.ramp_delay = 12000;
  329. break;
  330. case MAX8973_RAMP_25mV_PER_US:
  331. max->desc.ramp_delay = 25000;
  332. break;
  333. case MAX8973_RAMP_50mV_PER_US:
  334. max->desc.ramp_delay = 50000;
  335. break;
  336. case MAX8973_RAMP_200mV_PER_US:
  337. max->desc.ramp_delay = 200000;
  338. break;
  339. }
  340. if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
  341. control1 |= MAX8973_SNS_ENABLE;
  342. if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
  343. control1 |= MAX8973_NFSR_ENABLE;
  344. if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
  345. control1 |= MAX8973_AD_ENABLE;
  346. if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE) {
  347. control1 |= MAX8973_BIAS_ENABLE;
  348. max->desc.enable_time = 20;
  349. } else {
  350. max->desc.enable_time = 240;
  351. }
  352. if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
  353. control1 |= MAX8973_FREQSHIFT_9PER;
  354. if ((pdata->junction_temp_warning == MAX77621_TJINT_WARNING_TEMP_120) &&
  355. (max->id == MAX77621))
  356. control2 |= MAX77621_T_JUNCTION_120;
  357. if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
  358. control2 |= MAX8973_DISCH_ENBABLE;
  359. /* Clock advance trip configuration */
  360. switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
  361. case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
  362. control2 |= MAX8973_CKKADV_TRIP_DISABLE;
  363. break;
  364. case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
  365. control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
  366. break;
  367. case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
  368. control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
  369. break;
  370. case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
  371. control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
  372. break;
  373. }
  374. /* Configure inductor value */
  375. switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
  376. case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
  377. control2 |= MAX8973_INDUCTOR_NOMINAL;
  378. break;
  379. case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
  380. control2 |= MAX8973_INDUCTOR_MIN_30_PER;
  381. break;
  382. case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
  383. control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
  384. break;
  385. case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
  386. control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
  387. break;
  388. }
  389. ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
  390. if (ret < 0) {
  391. dev_err(max->dev, "register %d write failed, err = %d",
  392. MAX8973_CONTROL1, ret);
  393. return ret;
  394. }
  395. ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
  396. if (ret < 0) {
  397. dev_err(max->dev, "register %d write failed, err = %d",
  398. MAX8973_CONTROL2, ret);
  399. return ret;
  400. }
  401. /* If external control is enabled then disable EN bit */
  402. if (max->enable_external_control && (max->id == MAX8973)) {
  403. ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
  404. MAX8973_VOUT_ENABLE, 0);
  405. if (ret < 0)
  406. dev_err(max->dev, "register %d update failed, err = %d",
  407. MAX8973_VOUT, ret);
  408. }
  409. return ret;
  410. }
  411. static int max8973_thermal_read_temp(void *data, int *temp)
  412. {
  413. struct max8973_chip *mchip = data;
  414. unsigned int val;
  415. int ret;
  416. ret = regmap_read(mchip->regmap, MAX8973_CHIPID1, &val);
  417. if (ret < 0) {
  418. dev_err(mchip->dev, "Failed to read register CHIPID1, %d", ret);
  419. return ret;
  420. }
  421. /* +1 degC to trigger cool devive */
  422. if (val & MAX77621_CHIPID_TJINT_S)
  423. *temp = mchip->junction_temp_warning + 1000;
  424. else
  425. *temp = MAX77621_NORMAL_OPERATING_TEMP;
  426. return 0;
  427. }
  428. static irqreturn_t max8973_thermal_irq(int irq, void *data)
  429. {
  430. struct max8973_chip *mchip = data;
  431. thermal_zone_device_update(mchip->tz_device,
  432. THERMAL_EVENT_UNSPECIFIED);
  433. return IRQ_HANDLED;
  434. }
  435. static const struct thermal_zone_of_device_ops max77621_tz_ops = {
  436. .get_temp = max8973_thermal_read_temp,
  437. };
  438. static int max8973_thermal_init(struct max8973_chip *mchip)
  439. {
  440. struct thermal_zone_device *tzd;
  441. struct irq_data *irq_data;
  442. unsigned long irq_flags = 0;
  443. int ret;
  444. if (mchip->id != MAX77621)
  445. return 0;
  446. tzd = devm_thermal_zone_of_sensor_register(mchip->dev, 0, mchip,
  447. &max77621_tz_ops);
  448. if (IS_ERR(tzd)) {
  449. ret = PTR_ERR(tzd);
  450. dev_err(mchip->dev, "Failed to register thermal sensor: %d\n",
  451. ret);
  452. return ret;
  453. }
  454. if (mchip->irq <= 0)
  455. return 0;
  456. irq_data = irq_get_irq_data(mchip->irq);
  457. if (irq_data)
  458. irq_flags = irqd_get_trigger_type(irq_data);
  459. ret = devm_request_threaded_irq(mchip->dev, mchip->irq, NULL,
  460. max8973_thermal_irq,
  461. IRQF_ONESHOT | IRQF_SHARED | irq_flags,
  462. dev_name(mchip->dev), mchip);
  463. if (ret < 0) {
  464. dev_err(mchip->dev, "Failed to request irq %d, %d\n",
  465. mchip->irq, ret);
  466. return ret;
  467. }
  468. return 0;
  469. }
  470. static const struct regmap_config max8973_regmap_config = {
  471. .reg_bits = 8,
  472. .val_bits = 8,
  473. .max_register = MAX8973_CHIPID2,
  474. .cache_type = REGCACHE_RBTREE,
  475. };
  476. static struct max8973_regulator_platform_data *max8973_parse_dt(
  477. struct device *dev)
  478. {
  479. struct max8973_regulator_platform_data *pdata;
  480. struct device_node *np = dev->of_node;
  481. int ret;
  482. u32 pval;
  483. bool etr_enable;
  484. bool etr_sensitivity_high;
  485. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  486. if (!pdata)
  487. return NULL;
  488. pdata->enable_ext_control = of_property_read_bool(np,
  489. "maxim,externally-enable");
  490. pdata->enable_gpio = of_get_named_gpio(np, "maxim,enable-gpio", 0);
  491. pdata->dvs_gpio = of_get_named_gpio(np, "maxim,dvs-gpio", 0);
  492. ret = of_property_read_u32(np, "maxim,dvs-default-state", &pval);
  493. if (!ret)
  494. pdata->dvs_def_state = pval;
  495. if (of_property_read_bool(np, "maxim,enable-remote-sense"))
  496. pdata->control_flags |= MAX8973_CONTROL_REMOTE_SENSE_ENABLE;
  497. if (of_property_read_bool(np, "maxim,enable-falling-slew-rate"))
  498. pdata->control_flags |=
  499. MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE;
  500. if (of_property_read_bool(np, "maxim,enable-active-discharge"))
  501. pdata->control_flags |=
  502. MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE;
  503. if (of_property_read_bool(np, "maxim,enable-frequency-shift"))
  504. pdata->control_flags |= MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE;
  505. if (of_property_read_bool(np, "maxim,enable-bias-control"))
  506. pdata->control_flags |= MAX8973_CONTROL_BIAS_ENABLE;
  507. etr_enable = of_property_read_bool(np, "maxim,enable-etr");
  508. etr_sensitivity_high = of_property_read_bool(np,
  509. "maxim,enable-high-etr-sensitivity");
  510. if (etr_sensitivity_high)
  511. etr_enable = true;
  512. if (etr_enable) {
  513. if (etr_sensitivity_high)
  514. pdata->control_flags |=
  515. MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US;
  516. else
  517. pdata->control_flags |=
  518. MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US;
  519. } else {
  520. pdata->control_flags |= MAX8973_CONTROL_CLKADV_TRIP_DISABLED;
  521. }
  522. pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_140;
  523. ret = of_property_read_u32(np, "junction-warn-millicelsius", &pval);
  524. if (!ret && (pval <= MAX77621_TJINT_WARNING_TEMP_120))
  525. pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_120;
  526. return pdata;
  527. }
  528. static const struct of_device_id of_max8973_match_tbl[] = {
  529. { .compatible = "maxim,max8973", .data = (void *)MAX8973, },
  530. { .compatible = "maxim,max77621", .data = (void *)MAX77621, },
  531. { },
  532. };
  533. MODULE_DEVICE_TABLE(of, of_max8973_match_tbl);
  534. static int max8973_probe(struct i2c_client *client,
  535. const struct i2c_device_id *id)
  536. {
  537. struct max8973_regulator_platform_data *pdata;
  538. struct regulator_init_data *ridata;
  539. struct regulator_config config = { };
  540. struct regulator_dev *rdev;
  541. struct max8973_chip *max;
  542. bool pdata_from_dt = false;
  543. unsigned int chip_id;
  544. int ret;
  545. pdata = dev_get_platdata(&client->dev);
  546. if (!pdata && client->dev.of_node) {
  547. pdata = max8973_parse_dt(&client->dev);
  548. pdata_from_dt = true;
  549. }
  550. if (!pdata) {
  551. dev_err(&client->dev, "No Platform data");
  552. return -EIO;
  553. }
  554. if ((pdata->dvs_gpio == -EPROBE_DEFER) ||
  555. (pdata->enable_gpio == -EPROBE_DEFER))
  556. return -EPROBE_DEFER;
  557. max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
  558. if (!max)
  559. return -ENOMEM;
  560. max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
  561. if (IS_ERR(max->regmap)) {
  562. ret = PTR_ERR(max->regmap);
  563. dev_err(&client->dev, "regmap init failed, err %d\n", ret);
  564. return ret;
  565. }
  566. if (client->dev.of_node) {
  567. const struct of_device_id *match;
  568. match = of_match_device(of_match_ptr(of_max8973_match_tbl),
  569. &client->dev);
  570. if (!match)
  571. return -ENODATA;
  572. max->id = (u32)((uintptr_t)match->data);
  573. } else {
  574. max->id = id->driver_data;
  575. }
  576. ret = regmap_read(max->regmap, MAX8973_CHIPID1, &chip_id);
  577. if (ret < 0) {
  578. dev_err(&client->dev, "register CHIPID1 read failed, %d", ret);
  579. return ret;
  580. }
  581. dev_info(&client->dev, "CHIP-ID OTP: 0x%02x ID_M: 0x%02x\n",
  582. (chip_id >> 4) & 0xF, (chip_id >> 1) & 0x7);
  583. i2c_set_clientdata(client, max);
  584. max->ops = max8973_dcdc_ops;
  585. max->dev = &client->dev;
  586. max->desc.name = id->name;
  587. max->desc.id = 0;
  588. max->desc.ops = &max->ops;
  589. max->desc.type = REGULATOR_VOLTAGE;
  590. max->desc.owner = THIS_MODULE;
  591. max->desc.min_uV = MAX8973_MIN_VOLATGE;
  592. max->desc.uV_step = MAX8973_VOLATGE_STEP;
  593. max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
  594. max->dvs_gpio = (pdata->dvs_gpio) ? pdata->dvs_gpio : -EINVAL;
  595. max->enable_gpio = (pdata->enable_gpio) ? pdata->enable_gpio : -EINVAL;
  596. max->enable_external_control = pdata->enable_ext_control;
  597. max->curr_gpio_val = pdata->dvs_def_state;
  598. max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
  599. max->junction_temp_warning = pdata->junction_temp_warning;
  600. if (gpio_is_valid(max->enable_gpio))
  601. max->enable_external_control = true;
  602. max->lru_index[0] = max->curr_vout_reg;
  603. if (gpio_is_valid(max->dvs_gpio)) {
  604. int gpio_flags;
  605. int i;
  606. gpio_flags = (pdata->dvs_def_state) ?
  607. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  608. ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
  609. gpio_flags, "max8973-dvs");
  610. if (ret) {
  611. dev_err(&client->dev,
  612. "gpio_request for gpio %d failed, err = %d\n",
  613. max->dvs_gpio, ret);
  614. return ret;
  615. }
  616. /*
  617. * Initialize the lru index with vout_reg id
  618. * The index 0 will be most recently used and
  619. * set with the max->curr_vout_reg */
  620. for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
  621. max->lru_index[i] = i;
  622. max->lru_index[0] = max->curr_vout_reg;
  623. max->lru_index[max->curr_vout_reg] = 0;
  624. } else {
  625. /*
  626. * If there is no DVS GPIO, the VOUT register
  627. * address is fixed.
  628. */
  629. max->ops.set_voltage_sel = regulator_set_voltage_sel_regmap;
  630. max->ops.get_voltage_sel = regulator_get_voltage_sel_regmap;
  631. max->desc.vsel_reg = max->curr_vout_reg;
  632. max->desc.vsel_mask = MAX8973_VOUT_MASK;
  633. }
  634. if (pdata_from_dt)
  635. pdata->reg_init_data = of_get_regulator_init_data(&client->dev,
  636. client->dev.of_node, &max->desc);
  637. ridata = pdata->reg_init_data;
  638. switch (max->id) {
  639. case MAX8973:
  640. if (!pdata->enable_ext_control) {
  641. max->desc.enable_reg = MAX8973_VOUT;
  642. max->desc.enable_mask = MAX8973_VOUT_ENABLE;
  643. max->ops.enable = regulator_enable_regmap;
  644. max->ops.disable = regulator_disable_regmap;
  645. max->ops.is_enabled = regulator_is_enabled_regmap;
  646. break;
  647. }
  648. if (gpio_is_valid(max->enable_gpio)) {
  649. config.ena_gpio_flags = GPIOF_OUT_INIT_LOW;
  650. if (ridata && (ridata->constraints.always_on ||
  651. ridata->constraints.boot_on))
  652. config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
  653. config.ena_gpio = max->enable_gpio;
  654. }
  655. break;
  656. case MAX77621:
  657. if (gpio_is_valid(max->enable_gpio)) {
  658. ret = devm_gpio_request_one(&client->dev,
  659. max->enable_gpio, GPIOF_OUT_INIT_HIGH,
  660. "max8973-en-gpio");
  661. if (ret) {
  662. dev_err(&client->dev,
  663. "gpio_request for gpio %d failed: %d\n",
  664. max->enable_gpio, ret);
  665. return ret;
  666. }
  667. }
  668. max->desc.enable_reg = MAX8973_VOUT;
  669. max->desc.enable_mask = MAX8973_VOUT_ENABLE;
  670. max->ops.enable = regulator_enable_regmap;
  671. max->ops.disable = regulator_disable_regmap;
  672. max->ops.is_enabled = regulator_is_enabled_regmap;
  673. max->ops.set_current_limit = max8973_set_current_limit;
  674. max->ops.get_current_limit = max8973_get_current_limit;
  675. break;
  676. default:
  677. break;
  678. }
  679. ret = max8973_init_dcdc(max, pdata);
  680. if (ret < 0) {
  681. dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
  682. return ret;
  683. }
  684. config.dev = &client->dev;
  685. config.init_data = pdata->reg_init_data;
  686. config.driver_data = max;
  687. config.of_node = client->dev.of_node;
  688. config.regmap = max->regmap;
  689. /* Register the regulators */
  690. rdev = devm_regulator_register(&client->dev, &max->desc, &config);
  691. if (IS_ERR(rdev)) {
  692. ret = PTR_ERR(rdev);
  693. dev_err(max->dev, "regulator register failed, err %d\n", ret);
  694. return ret;
  695. }
  696. max8973_thermal_init(max);
  697. return 0;
  698. }
  699. static const struct i2c_device_id max8973_id[] = {
  700. {.name = "max8973", .driver_data = MAX8973},
  701. {.name = "max77621", .driver_data = MAX77621},
  702. {},
  703. };
  704. MODULE_DEVICE_TABLE(i2c, max8973_id);
  705. static struct i2c_driver max8973_i2c_driver = {
  706. .driver = {
  707. .name = "max8973",
  708. .of_match_table = of_max8973_match_tbl,
  709. },
  710. .probe = max8973_probe,
  711. .id_table = max8973_id,
  712. };
  713. static int __init max8973_init(void)
  714. {
  715. return i2c_add_driver(&max8973_i2c_driver);
  716. }
  717. subsys_initcall(max8973_init);
  718. static void __exit max8973_cleanup(void)
  719. {
  720. i2c_del_driver(&max8973_i2c_driver);
  721. }
  722. module_exit(max8973_cleanup);
  723. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  724. MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
  725. MODULE_LICENSE("GPL v2");