tps65218.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for TPS65218 Integrated power management chipsets
  4. *
  5. * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/init.h>
  12. #include <linux/i2c.h>
  13. #include <linux/slab.h>
  14. #include <linux/regmap.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mutex.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/tps65218.h>
  23. #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D
  24. static const struct mfd_cell tps65218_cells[] = {
  25. {
  26. .name = "tps65218-pwrbutton",
  27. .of_compatible = "ti,tps65218-pwrbutton",
  28. },
  29. {
  30. .name = "tps65218-gpio",
  31. .of_compatible = "ti,tps65218-gpio",
  32. },
  33. { .name = "tps65218-regulator", },
  34. };
  35. /**
  36. * tps65218_reg_write: Write a single tps65218 register.
  37. *
  38. * @tps: Device to write to.
  39. * @reg: Register to write to.
  40. * @val: Value to write.
  41. * @level: Password protected level
  42. */
  43. int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
  44. unsigned int val, unsigned int level)
  45. {
  46. int ret;
  47. unsigned int xor_reg_val;
  48. switch (level) {
  49. case TPS65218_PROTECT_NONE:
  50. return regmap_write(tps->regmap, reg, val);
  51. case TPS65218_PROTECT_L1:
  52. xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
  53. ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
  54. xor_reg_val);
  55. if (ret < 0)
  56. return ret;
  57. return regmap_write(tps->regmap, reg, val);
  58. default:
  59. return -EINVAL;
  60. }
  61. }
  62. EXPORT_SYMBOL_GPL(tps65218_reg_write);
  63. /**
  64. * tps65218_update_bits: Modify bits w.r.t mask, val and level.
  65. *
  66. * @tps: Device to write to.
  67. * @reg: Register to read-write to.
  68. * @mask: Mask.
  69. * @val: Value to write.
  70. * @level: Password protected level
  71. */
  72. static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
  73. unsigned int mask, unsigned int val, unsigned int level)
  74. {
  75. int ret;
  76. unsigned int data;
  77. ret = regmap_read(tps->regmap, reg, &data);
  78. if (ret) {
  79. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  80. return ret;
  81. }
  82. data &= ~mask;
  83. data |= val & mask;
  84. mutex_lock(&tps->tps_lock);
  85. ret = tps65218_reg_write(tps, reg, data, level);
  86. if (ret)
  87. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  88. mutex_unlock(&tps->tps_lock);
  89. return ret;
  90. }
  91. int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
  92. unsigned int mask, unsigned int val, unsigned int level)
  93. {
  94. return tps65218_update_bits(tps, reg, mask, val, level);
  95. }
  96. EXPORT_SYMBOL_GPL(tps65218_set_bits);
  97. int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
  98. unsigned int mask, unsigned int level)
  99. {
  100. return tps65218_update_bits(tps, reg, mask, 0, level);
  101. }
  102. EXPORT_SYMBOL_GPL(tps65218_clear_bits);
  103. static const struct regmap_range tps65218_yes_ranges[] = {
  104. regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
  105. regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
  106. };
  107. static const struct regmap_access_table tps65218_volatile_table = {
  108. .yes_ranges = tps65218_yes_ranges,
  109. .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
  110. };
  111. static const struct regmap_config tps65218_regmap_config = {
  112. .reg_bits = 8,
  113. .val_bits = 8,
  114. .cache_type = REGCACHE_RBTREE,
  115. .volatile_table = &tps65218_volatile_table,
  116. };
  117. static const struct regmap_irq tps65218_irqs[] = {
  118. /* INT1 IRQs */
  119. [TPS65218_PRGC_IRQ] = {
  120. .mask = TPS65218_INT1_PRGC,
  121. },
  122. [TPS65218_CC_AQC_IRQ] = {
  123. .mask = TPS65218_INT1_CC_AQC,
  124. },
  125. [TPS65218_HOT_IRQ] = {
  126. .mask = TPS65218_INT1_HOT,
  127. },
  128. [TPS65218_PB_IRQ] = {
  129. .mask = TPS65218_INT1_PB,
  130. },
  131. [TPS65218_AC_IRQ] = {
  132. .mask = TPS65218_INT1_AC,
  133. },
  134. [TPS65218_VPRG_IRQ] = {
  135. .mask = TPS65218_INT1_VPRG,
  136. },
  137. [TPS65218_INVALID1_IRQ] = {
  138. },
  139. [TPS65218_INVALID2_IRQ] = {
  140. },
  141. /* INT2 IRQs*/
  142. [TPS65218_LS1_I_IRQ] = {
  143. .mask = TPS65218_INT2_LS1_I,
  144. .reg_offset = 1,
  145. },
  146. [TPS65218_LS2_I_IRQ] = {
  147. .mask = TPS65218_INT2_LS2_I,
  148. .reg_offset = 1,
  149. },
  150. [TPS65218_LS3_I_IRQ] = {
  151. .mask = TPS65218_INT2_LS3_I,
  152. .reg_offset = 1,
  153. },
  154. [TPS65218_LS1_F_IRQ] = {
  155. .mask = TPS65218_INT2_LS1_F,
  156. .reg_offset = 1,
  157. },
  158. [TPS65218_LS2_F_IRQ] = {
  159. .mask = TPS65218_INT2_LS2_F,
  160. .reg_offset = 1,
  161. },
  162. [TPS65218_LS3_F_IRQ] = {
  163. .mask = TPS65218_INT2_LS3_F,
  164. .reg_offset = 1,
  165. },
  166. [TPS65218_INVALID3_IRQ] = {
  167. },
  168. [TPS65218_INVALID4_IRQ] = {
  169. },
  170. };
  171. static struct regmap_irq_chip tps65218_irq_chip = {
  172. .name = "tps65218",
  173. .irqs = tps65218_irqs,
  174. .num_irqs = ARRAY_SIZE(tps65218_irqs),
  175. .num_regs = 2,
  176. .mask_base = TPS65218_REG_INT_MASK1,
  177. .status_base = TPS65218_REG_INT1,
  178. };
  179. static const struct of_device_id of_tps65218_match_table[] = {
  180. { .compatible = "ti,tps65218", },
  181. {}
  182. };
  183. MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
  184. static int tps65218_voltage_set_strict(struct tps65218 *tps)
  185. {
  186. u32 strict;
  187. if (of_property_read_u32(tps->dev->of_node,
  188. "ti,strict-supply-voltage-supervision",
  189. &strict))
  190. return 0;
  191. if (strict != 0 && strict != 1) {
  192. dev_err(tps->dev,
  193. "Invalid ti,strict-supply-voltage-supervision value\n");
  194. return -EINVAL;
  195. }
  196. tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
  197. TPS65218_CONFIG1_STRICT,
  198. strict ? TPS65218_CONFIG1_STRICT : 0,
  199. TPS65218_PROTECT_L1);
  200. return 0;
  201. }
  202. static int tps65218_voltage_set_uv_hyst(struct tps65218 *tps)
  203. {
  204. u32 hyst;
  205. if (of_property_read_u32(tps->dev->of_node,
  206. "ti,under-voltage-hyst-microvolt", &hyst))
  207. return 0;
  208. if (hyst != 400000 && hyst != 200000) {
  209. dev_err(tps->dev,
  210. "Invalid ti,under-voltage-hyst-microvolt value\n");
  211. return -EINVAL;
  212. }
  213. tps65218_update_bits(tps, TPS65218_REG_CONFIG2,
  214. TPS65218_CONFIG2_UVLOHYS,
  215. hyst == 400000 ? TPS65218_CONFIG2_UVLOHYS : 0,
  216. TPS65218_PROTECT_L1);
  217. return 0;
  218. }
  219. static int tps65218_voltage_set_uvlo(struct tps65218 *tps)
  220. {
  221. u32 uvlo;
  222. int uvloval;
  223. if (of_property_read_u32(tps->dev->of_node,
  224. "ti,under-voltage-limit-microvolt", &uvlo))
  225. return 0;
  226. switch (uvlo) {
  227. case 2750000:
  228. uvloval = TPS65218_CONFIG1_UVLO_2750000;
  229. break;
  230. case 2950000:
  231. uvloval = TPS65218_CONFIG1_UVLO_2950000;
  232. break;
  233. case 3250000:
  234. uvloval = TPS65218_CONFIG1_UVLO_3250000;
  235. break;
  236. case 3350000:
  237. uvloval = TPS65218_CONFIG1_UVLO_3350000;
  238. break;
  239. default:
  240. dev_err(tps->dev,
  241. "Invalid ti,under-voltage-limit-microvolt value\n");
  242. return -EINVAL;
  243. }
  244. tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
  245. TPS65218_CONFIG1_UVLO_MASK, uvloval,
  246. TPS65218_PROTECT_L1);
  247. return 0;
  248. }
  249. static int tps65218_probe(struct i2c_client *client,
  250. const struct i2c_device_id *ids)
  251. {
  252. struct tps65218 *tps;
  253. int ret;
  254. unsigned int chipid;
  255. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  256. if (!tps)
  257. return -ENOMEM;
  258. i2c_set_clientdata(client, tps);
  259. tps->dev = &client->dev;
  260. tps->irq = client->irq;
  261. tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
  262. if (IS_ERR(tps->regmap)) {
  263. ret = PTR_ERR(tps->regmap);
  264. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  265. ret);
  266. return ret;
  267. }
  268. mutex_init(&tps->tps_lock);
  269. ret = devm_regmap_add_irq_chip(&client->dev, tps->regmap, tps->irq,
  270. IRQF_ONESHOT, 0, &tps65218_irq_chip,
  271. &tps->irq_data);
  272. if (ret < 0)
  273. return ret;
  274. ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
  275. if (ret) {
  276. dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
  277. return ret;
  278. }
  279. tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
  280. ret = tps65218_voltage_set_strict(tps);
  281. if (ret)
  282. return ret;
  283. ret = tps65218_voltage_set_uvlo(tps);
  284. if (ret)
  285. return ret;
  286. ret = tps65218_voltage_set_uv_hyst(tps);
  287. if (ret)
  288. return ret;
  289. ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
  290. ARRAY_SIZE(tps65218_cells), NULL, 0,
  291. regmap_irq_get_domain(tps->irq_data));
  292. return ret;
  293. }
  294. static const struct i2c_device_id tps65218_id_table[] = {
  295. { "tps65218", TPS65218 },
  296. { },
  297. };
  298. MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
  299. static struct i2c_driver tps65218_driver = {
  300. .driver = {
  301. .name = "tps65218",
  302. .of_match_table = of_tps65218_match_table,
  303. },
  304. .probe = tps65218_probe,
  305. .id_table = tps65218_id_table,
  306. };
  307. module_i2c_driver(tps65218_driver);
  308. MODULE_AUTHOR("J Keerthy <[email protected]>");
  309. MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
  310. MODULE_LICENSE("GPL v2");