slg51000-regulator.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // SLG51000 High PSRR, Multi-Output Regulators
  4. // Copyright (C) 2019 Dialog Semiconductor
  5. //
  6. // Author: Eric Jeong <eric.jeong.opensource@diasemi.com>
  7. #include <linux/err.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/i2c.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #include "slg51000-regulator.h"
  20. #define SLG51000_SCTL_EVT 7
  21. #define SLG51000_MAX_EVT_REGISTER 8
  22. #define SLG51000_LDOHP_LV_MIN 1200000
  23. #define SLG51000_LDOHP_HV_MIN 2400000
  24. enum slg51000_regulators {
  25. SLG51000_REGULATOR_LDO1 = 0,
  26. SLG51000_REGULATOR_LDO2,
  27. SLG51000_REGULATOR_LDO3,
  28. SLG51000_REGULATOR_LDO4,
  29. SLG51000_REGULATOR_LDO5,
  30. SLG51000_REGULATOR_LDO6,
  31. SLG51000_REGULATOR_LDO7,
  32. SLG51000_MAX_REGULATORS,
  33. };
  34. struct slg51000 {
  35. struct device *dev;
  36. struct regmap *regmap;
  37. struct regulator_desc *rdesc[SLG51000_MAX_REGULATORS];
  38. struct regulator_dev *rdev[SLG51000_MAX_REGULATORS];
  39. struct gpio_desc *cs_gpiod;
  40. int chip_irq;
  41. };
  42. struct slg51000_evt_sta {
  43. unsigned int ereg;
  44. unsigned int sreg;
  45. };
  46. static const struct slg51000_evt_sta es_reg[SLG51000_MAX_EVT_REGISTER] = {
  47. {SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS},
  48. {SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS},
  49. {SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS},
  50. {SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS},
  51. {SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS},
  52. {SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS},
  53. {SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS},
  54. {SLG51000_SYSCTL_EVENT, SLG51000_SYSCTL_STATUS},
  55. };
  56. static const struct regmap_range slg51000_writeable_ranges[] = {
  57. regmap_reg_range(SLG51000_SYSCTL_MATRIX_CONF_A,
  58. SLG51000_SYSCTL_MATRIX_CONF_A),
  59. regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
  60. regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
  61. regmap_reg_range(SLG51000_LDO1_IRQ_MASK, SLG51000_LDO1_IRQ_MASK),
  62. regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
  63. regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
  64. regmap_reg_range(SLG51000_LDO2_IRQ_MASK, SLG51000_LDO2_IRQ_MASK),
  65. regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
  66. regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
  67. regmap_reg_range(SLG51000_LDO3_IRQ_MASK, SLG51000_LDO3_IRQ_MASK),
  68. regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
  69. regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
  70. regmap_reg_range(SLG51000_LDO4_IRQ_MASK, SLG51000_LDO4_IRQ_MASK),
  71. regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
  72. regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
  73. regmap_reg_range(SLG51000_LDO5_IRQ_MASK, SLG51000_LDO5_IRQ_MASK),
  74. regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
  75. regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
  76. regmap_reg_range(SLG51000_LDO6_IRQ_MASK, SLG51000_LDO6_IRQ_MASK),
  77. regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
  78. regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
  79. regmap_reg_range(SLG51000_LDO7_IRQ_MASK, SLG51000_LDO7_IRQ_MASK),
  80. regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
  81. };
  82. static const struct regmap_range slg51000_readable_ranges[] = {
  83. regmap_reg_range(SLG51000_SYSCTL_PATN_ID_B0,
  84. SLG51000_SYSCTL_PATN_ID_B2),
  85. regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_A,
  86. SLG51000_SYSCTL_SYS_CONF_A),
  87. regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_D,
  88. SLG51000_SYSCTL_MATRIX_CONF_B),
  89. regmap_reg_range(SLG51000_SYSCTL_REFGEN_CONF_C,
  90. SLG51000_SYSCTL_UVLO_CONF_A),
  91. regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_IRQ_MASK),
  92. regmap_reg_range(SLG51000_IO_GPIO1_CONF, SLG51000_IO_GPIO_STATUS),
  93. regmap_reg_range(SLG51000_LUTARRAY_LUT_VAL_0,
  94. SLG51000_LUTARRAY_LUT_VAL_11),
  95. regmap_reg_range(SLG51000_MUXARRAY_INPUT_SEL_0,
  96. SLG51000_MUXARRAY_INPUT_SEL_63),
  97. regmap_reg_range(SLG51000_PWRSEQ_RESOURCE_EN_0,
  98. SLG51000_PWRSEQ_INPUT_SENSE_CONF_B),
  99. regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
  100. regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
  101. regmap_reg_range(SLG51000_LDO1_MISC1, SLG51000_LDO1_VSEL_ACTUAL),
  102. regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_IRQ_MASK),
  103. regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
  104. regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
  105. regmap_reg_range(SLG51000_LDO2_MISC1, SLG51000_LDO2_VSEL_ACTUAL),
  106. regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_IRQ_MASK),
  107. regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
  108. regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
  109. regmap_reg_range(SLG51000_LDO3_CONF1, SLG51000_LDO3_VSEL_ACTUAL),
  110. regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_IRQ_MASK),
  111. regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
  112. regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
  113. regmap_reg_range(SLG51000_LDO4_CONF1, SLG51000_LDO4_VSEL_ACTUAL),
  114. regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_IRQ_MASK),
  115. regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
  116. regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
  117. regmap_reg_range(SLG51000_LDO5_TRIM2, SLG51000_LDO5_TRIM2),
  118. regmap_reg_range(SLG51000_LDO5_CONF1, SLG51000_LDO5_VSEL_ACTUAL),
  119. regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_IRQ_MASK),
  120. regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
  121. regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
  122. regmap_reg_range(SLG51000_LDO6_TRIM2, SLG51000_LDO6_TRIM2),
  123. regmap_reg_range(SLG51000_LDO6_CONF1, SLG51000_LDO6_VSEL_ACTUAL),
  124. regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_IRQ_MASK),
  125. regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
  126. regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
  127. regmap_reg_range(SLG51000_LDO7_CONF1, SLG51000_LDO7_VSEL_ACTUAL),
  128. regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_IRQ_MASK),
  129. regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
  130. regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
  131. regmap_reg_range(SLG51000_OTP_LOCK_OTP_PROG, SLG51000_OTP_LOCK_CTRL),
  132. regmap_reg_range(SLG51000_LOCK_GLOBAL_LOCK_CTRL1,
  133. SLG51000_LOCK_GLOBAL_LOCK_CTRL1),
  134. };
  135. static const struct regmap_range slg51000_volatile_ranges[] = {
  136. regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_STATUS),
  137. regmap_reg_range(SLG51000_IO_GPIO_STATUS, SLG51000_IO_GPIO_STATUS),
  138. regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS),
  139. regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS),
  140. regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS),
  141. regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS),
  142. regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS),
  143. regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS),
  144. regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS),
  145. regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
  146. };
  147. static const struct regmap_access_table slg51000_writeable_table = {
  148. .yes_ranges = slg51000_writeable_ranges,
  149. .n_yes_ranges = ARRAY_SIZE(slg51000_writeable_ranges),
  150. };
  151. static const struct regmap_access_table slg51000_readable_table = {
  152. .yes_ranges = slg51000_readable_ranges,
  153. .n_yes_ranges = ARRAY_SIZE(slg51000_readable_ranges),
  154. };
  155. static const struct regmap_access_table slg51000_volatile_table = {
  156. .yes_ranges = slg51000_volatile_ranges,
  157. .n_yes_ranges = ARRAY_SIZE(slg51000_volatile_ranges),
  158. };
  159. static const struct regmap_config slg51000_regmap_config = {
  160. .reg_bits = 16,
  161. .val_bits = 8,
  162. .max_register = 0x8000,
  163. .wr_table = &slg51000_writeable_table,
  164. .rd_table = &slg51000_readable_table,
  165. .volatile_table = &slg51000_volatile_table,
  166. };
  167. static const struct regulator_ops slg51000_regl_ops = {
  168. .enable = regulator_enable_regmap,
  169. .disable = regulator_disable_regmap,
  170. .is_enabled = regulator_is_enabled_regmap,
  171. .list_voltage = regulator_list_voltage_linear,
  172. .map_voltage = regulator_map_voltage_linear,
  173. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  174. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  175. };
  176. static const struct regulator_ops slg51000_switch_ops = {
  177. .enable = regulator_enable_regmap,
  178. .disable = regulator_disable_regmap,
  179. .is_enabled = regulator_is_enabled_regmap,
  180. };
  181. static int slg51000_of_parse_cb(struct device_node *np,
  182. const struct regulator_desc *desc,
  183. struct regulator_config *config)
  184. {
  185. struct slg51000 *chip = config->driver_data;
  186. struct gpio_desc *ena_gpiod;
  187. enum gpiod_flags gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  188. ena_gpiod = devm_gpiod_get_from_of_node(chip->dev, np,
  189. "enable-gpios", 0,
  190. gflags, "gpio-en-ldo");
  191. if (!IS_ERR(ena_gpiod)) {
  192. config->ena_gpiod = ena_gpiod;
  193. devm_gpiod_unhinge(chip->dev, config->ena_gpiod);
  194. }
  195. return 0;
  196. }
  197. #define SLG51000_REGL_DESC(_id, _name, _s_name, _min, _step) \
  198. [SLG51000_REGULATOR_##_id] = { \
  199. .name = #_name, \
  200. .supply_name = _s_name, \
  201. .id = SLG51000_REGULATOR_##_id, \
  202. .of_match = of_match_ptr(#_name), \
  203. .of_parse_cb = slg51000_of_parse_cb, \
  204. .ops = &slg51000_regl_ops, \
  205. .regulators_node = of_match_ptr("regulators"), \
  206. .n_voltages = 256, \
  207. .min_uV = _min, \
  208. .uV_step = _step, \
  209. .linear_min_sel = 0, \
  210. .vsel_mask = SLG51000_VSEL_MASK, \
  211. .vsel_reg = SLG51000_##_id##_VSEL, \
  212. .enable_reg = SLG51000_SYSCTL_MATRIX_CONF_A, \
  213. .enable_mask = BIT(SLG51000_REGULATOR_##_id), \
  214. .type = REGULATOR_VOLTAGE, \
  215. .owner = THIS_MODULE, \
  216. }
  217. static struct regulator_desc regls_desc[SLG51000_MAX_REGULATORS] = {
  218. SLG51000_REGL_DESC(LDO1, ldo1, NULL, 2400000, 5000),
  219. SLG51000_REGL_DESC(LDO2, ldo2, NULL, 2400000, 5000),
  220. SLG51000_REGL_DESC(LDO3, ldo3, "vin3", 1200000, 10000),
  221. SLG51000_REGL_DESC(LDO4, ldo4, "vin4", 1200000, 10000),
  222. SLG51000_REGL_DESC(LDO5, ldo5, "vin5", 400000, 5000),
  223. SLG51000_REGL_DESC(LDO6, ldo6, "vin6", 400000, 5000),
  224. SLG51000_REGL_DESC(LDO7, ldo7, "vin7", 1200000, 10000),
  225. };
  226. static int slg51000_regulator_init(struct slg51000 *chip)
  227. {
  228. struct regulator_config config = { };
  229. struct regulator_desc *rdesc;
  230. unsigned int reg, val;
  231. u8 vsel_range[2];
  232. int id, ret = 0;
  233. const unsigned int min_regs[SLG51000_MAX_REGULATORS] = {
  234. SLG51000_LDO1_MINV, SLG51000_LDO2_MINV, SLG51000_LDO3_MINV,
  235. SLG51000_LDO4_MINV, SLG51000_LDO5_MINV, SLG51000_LDO6_MINV,
  236. SLG51000_LDO7_MINV,
  237. };
  238. for (id = 0; id < SLG51000_MAX_REGULATORS; id++) {
  239. chip->rdesc[id] = &regls_desc[id];
  240. rdesc = chip->rdesc[id];
  241. config.regmap = chip->regmap;
  242. config.dev = chip->dev;
  243. config.driver_data = chip;
  244. ret = regmap_bulk_read(chip->regmap, min_regs[id],
  245. vsel_range, 2);
  246. if (ret < 0) {
  247. dev_err(chip->dev,
  248. "Failed to read the MIN register\n");
  249. return ret;
  250. }
  251. switch (id) {
  252. case SLG51000_REGULATOR_LDO1:
  253. case SLG51000_REGULATOR_LDO2:
  254. if (id == SLG51000_REGULATOR_LDO1)
  255. reg = SLG51000_LDO1_MISC1;
  256. else
  257. reg = SLG51000_LDO2_MISC1;
  258. ret = regmap_read(chip->regmap, reg, &val);
  259. if (ret < 0) {
  260. dev_err(chip->dev,
  261. "Failed to read voltage range of ldo%d\n",
  262. id + 1);
  263. return ret;
  264. }
  265. rdesc->linear_min_sel = vsel_range[0];
  266. rdesc->n_voltages = vsel_range[1] + 1;
  267. if (val & SLG51000_SEL_VRANGE_MASK)
  268. rdesc->min_uV = SLG51000_LDOHP_HV_MIN
  269. + (vsel_range[0]
  270. * rdesc->uV_step);
  271. else
  272. rdesc->min_uV = SLG51000_LDOHP_LV_MIN
  273. + (vsel_range[0]
  274. * rdesc->uV_step);
  275. break;
  276. case SLG51000_REGULATOR_LDO5:
  277. case SLG51000_REGULATOR_LDO6:
  278. if (id == SLG51000_REGULATOR_LDO5)
  279. reg = SLG51000_LDO5_TRIM2;
  280. else
  281. reg = SLG51000_LDO6_TRIM2;
  282. ret = regmap_read(chip->regmap, reg, &val);
  283. if (ret < 0) {
  284. dev_err(chip->dev,
  285. "Failed to read LDO mode register\n");
  286. return ret;
  287. }
  288. if (val & SLG51000_SEL_BYP_MODE_MASK) {
  289. rdesc->ops = &slg51000_switch_ops;
  290. rdesc->n_voltages = 0;
  291. rdesc->min_uV = 0;
  292. rdesc->uV_step = 0;
  293. rdesc->linear_min_sel = 0;
  294. break;
  295. }
  296. /* Fall through - to the check below.*/
  297. default:
  298. rdesc->linear_min_sel = vsel_range[0];
  299. rdesc->n_voltages = vsel_range[1] + 1;
  300. rdesc->min_uV = rdesc->min_uV
  301. + (vsel_range[0] * rdesc->uV_step);
  302. break;
  303. }
  304. chip->rdev[id] = devm_regulator_register(chip->dev, rdesc,
  305. &config);
  306. if (IS_ERR(chip->rdev[id])) {
  307. ret = PTR_ERR(chip->rdev[id]);
  308. dev_err(chip->dev,
  309. "Failed to register regulator(%s):%d\n",
  310. chip->rdesc[id]->name, ret);
  311. return ret;
  312. }
  313. }
  314. return 0;
  315. }
  316. static irqreturn_t slg51000_irq_handler(int irq, void *data)
  317. {
  318. struct slg51000 *chip = data;
  319. struct regmap *regmap = chip->regmap;
  320. enum { R0 = 0, R1, R2, REG_MAX };
  321. u8 evt[SLG51000_MAX_EVT_REGISTER][REG_MAX];
  322. int ret, i, handled = IRQ_NONE;
  323. unsigned int evt_otp, mask_otp;
  324. /* Read event[R0], status[R1] and mask[R2] register */
  325. for (i = 0; i < SLG51000_MAX_EVT_REGISTER; i++) {
  326. ret = regmap_bulk_read(regmap, es_reg[i].ereg, evt[i], REG_MAX);
  327. if (ret < 0) {
  328. dev_err(chip->dev,
  329. "Failed to read event registers(%d)\n", ret);
  330. return IRQ_NONE;
  331. }
  332. }
  333. ret = regmap_read(regmap, SLG51000_OTP_EVENT, &evt_otp);
  334. if (ret < 0) {
  335. dev_err(chip->dev,
  336. "Failed to read otp event registers(%d)\n", ret);
  337. return IRQ_NONE;
  338. }
  339. ret = regmap_read(regmap, SLG51000_OTP_IRQ_MASK, &mask_otp);
  340. if (ret < 0) {
  341. dev_err(chip->dev,
  342. "Failed to read otp mask register(%d)\n", ret);
  343. return IRQ_NONE;
  344. }
  345. if ((evt_otp & SLG51000_EVT_CRC_MASK) &&
  346. !(mask_otp & SLG51000_IRQ_CRC_MASK)) {
  347. dev_info(chip->dev,
  348. "OTP has been read or OTP crc is not zero\n");
  349. handled = IRQ_HANDLED;
  350. }
  351. for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
  352. if (!(evt[i][R2] & SLG51000_IRQ_ILIM_FLAG_MASK) &&
  353. (evt[i][R0] & SLG51000_EVT_ILIM_FLAG_MASK)) {
  354. regulator_lock(chip->rdev[i]);
  355. regulator_notifier_call_chain(chip->rdev[i],
  356. REGULATOR_EVENT_OVER_CURRENT, NULL);
  357. regulator_unlock(chip->rdev[i]);
  358. if (evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK)
  359. dev_warn(chip->dev,
  360. "Over-current limit(ldo%d)\n", i + 1);
  361. handled = IRQ_HANDLED;
  362. }
  363. }
  364. if (!(evt[SLG51000_SCTL_EVT][R2] & SLG51000_IRQ_HIGH_TEMP_WARN_MASK) &&
  365. (evt[SLG51000_SCTL_EVT][R0] & SLG51000_EVT_HIGH_TEMP_WARN_MASK)) {
  366. for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
  367. if (!(evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK) &&
  368. (evt[i][R1] & SLG51000_STA_VOUT_OK_FLAG_MASK)) {
  369. regulator_lock(chip->rdev[i]);
  370. regulator_notifier_call_chain(chip->rdev[i],
  371. REGULATOR_EVENT_OVER_TEMP, NULL);
  372. regulator_unlock(chip->rdev[i]);
  373. }
  374. }
  375. handled = IRQ_HANDLED;
  376. if (evt[SLG51000_SCTL_EVT][R1] &
  377. SLG51000_STA_HIGH_TEMP_WARN_MASK)
  378. dev_warn(chip->dev, "High temperature warning!\n");
  379. }
  380. return handled;
  381. }
  382. static void slg51000_clear_fault_log(struct slg51000 *chip)
  383. {
  384. unsigned int val = 0;
  385. int ret = 0;
  386. ret = regmap_read(chip->regmap, SLG51000_SYSCTL_FAULT_LOG1, &val);
  387. if (ret < 0) {
  388. dev_err(chip->dev, "Failed to read Fault log register\n");
  389. return;
  390. }
  391. if (val & SLG51000_FLT_OVER_TEMP_MASK)
  392. dev_dbg(chip->dev, "Fault log: FLT_OVER_TEMP\n");
  393. if (val & SLG51000_FLT_POWER_SEQ_CRASH_REQ_MASK)
  394. dev_dbg(chip->dev, "Fault log: FLT_POWER_SEQ_CRASH_REQ\n");
  395. if (val & SLG51000_FLT_RST_MASK)
  396. dev_dbg(chip->dev, "Fault log: FLT_RST\n");
  397. if (val & SLG51000_FLT_POR_MASK)
  398. dev_dbg(chip->dev, "Fault log: FLT_POR\n");
  399. }
  400. static int slg51000_i2c_probe(struct i2c_client *client,
  401. const struct i2c_device_id *id)
  402. {
  403. struct device *dev = &client->dev;
  404. struct slg51000 *chip;
  405. struct gpio_desc *cs_gpiod;
  406. int error, ret;
  407. chip = devm_kzalloc(dev, sizeof(struct slg51000), GFP_KERNEL);
  408. if (!chip)
  409. return -ENOMEM;
  410. cs_gpiod = devm_gpiod_get_optional(dev, "dlg,cs",
  411. GPIOD_OUT_HIGH |
  412. GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  413. if (IS_ERR(cs_gpiod))
  414. return PTR_ERR(cs_gpiod);
  415. if (cs_gpiod) {
  416. dev_info(dev, "Found chip selector property\n");
  417. chip->cs_gpiod = cs_gpiod;
  418. }
  419. i2c_set_clientdata(client, chip);
  420. chip->chip_irq = client->irq;
  421. chip->dev = dev;
  422. chip->regmap = devm_regmap_init_i2c(client, &slg51000_regmap_config);
  423. if (IS_ERR(chip->regmap)) {
  424. error = PTR_ERR(chip->regmap);
  425. dev_err(dev, "Failed to allocate register map: %d\n",
  426. error);
  427. return error;
  428. }
  429. ret = slg51000_regulator_init(chip);
  430. if (ret < 0) {
  431. dev_err(chip->dev, "Failed to init regulator(%d)\n", ret);
  432. return ret;
  433. }
  434. slg51000_clear_fault_log(chip);
  435. if (chip->chip_irq) {
  436. ret = devm_request_threaded_irq(dev, chip->chip_irq, NULL,
  437. slg51000_irq_handler,
  438. (IRQF_TRIGGER_HIGH |
  439. IRQF_ONESHOT),
  440. "slg51000-irq", chip);
  441. if (ret != 0) {
  442. dev_err(dev, "Failed to request IRQ: %d\n",
  443. chip->chip_irq);
  444. return ret;
  445. }
  446. } else {
  447. dev_info(dev, "No IRQ configured\n");
  448. }
  449. return ret;
  450. }
  451. static const struct i2c_device_id slg51000_i2c_id[] = {
  452. {"slg51000", 0},
  453. {},
  454. };
  455. MODULE_DEVICE_TABLE(i2c, slg51000_i2c_id);
  456. static struct i2c_driver slg51000_regulator_driver = {
  457. .driver = {
  458. .name = "slg51000-regulator",
  459. },
  460. .probe = slg51000_i2c_probe,
  461. .id_table = slg51000_i2c_id,
  462. };
  463. module_i2c_driver(slg51000_regulator_driver);
  464. MODULE_AUTHOR("Eric Jeong <eric.jeong.opensource@diasemi.com>");
  465. MODULE_DESCRIPTION("SLG51000 regulator driver");
  466. MODULE_LICENSE("GPL");