tps65217.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * tps65217.c
  3. *
  4. * TPS65217 chip family multi-function driver
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/init.h>
  22. #include <linux/i2c.h>
  23. #include <linux/slab.h>
  24. #include <linux/regmap.h>
  25. #include <linux/err.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/tps65217.h>
  30. static struct mfd_cell tps65217s[] = {
  31. {
  32. .name = "tps65217-pmic",
  33. },
  34. {
  35. .name = "tps65217-bl",
  36. },
  37. };
  38. /**
  39. * tps65217_reg_read: Read a single tps65217 register.
  40. *
  41. * @tps: Device to read from.
  42. * @reg: Register to read.
  43. * @val: Contians the value
  44. */
  45. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  46. unsigned int *val)
  47. {
  48. return regmap_read(tps->regmap, reg, val);
  49. }
  50. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  51. /**
  52. * tps65217_reg_write: Write a single tps65217 register.
  53. *
  54. * @tps65217: Device to write to.
  55. * @reg: Register to write to.
  56. * @val: Value to write.
  57. * @level: Password protected level
  58. */
  59. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  60. unsigned int val, unsigned int level)
  61. {
  62. int ret;
  63. unsigned int xor_reg_val;
  64. switch (level) {
  65. case TPS65217_PROTECT_NONE:
  66. return regmap_write(tps->regmap, reg, val);
  67. case TPS65217_PROTECT_L1:
  68. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  69. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  70. xor_reg_val);
  71. if (ret < 0)
  72. return ret;
  73. return regmap_write(tps->regmap, reg, val);
  74. case TPS65217_PROTECT_L2:
  75. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  76. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  77. xor_reg_val);
  78. if (ret < 0)
  79. return ret;
  80. ret = regmap_write(tps->regmap, reg, val);
  81. if (ret < 0)
  82. return ret;
  83. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  84. xor_reg_val);
  85. if (ret < 0)
  86. return ret;
  87. return regmap_write(tps->regmap, reg, val);
  88. default:
  89. return -EINVAL;
  90. }
  91. }
  92. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  93. /**
  94. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  95. *
  96. * @tps65217: Device to write to.
  97. * @reg: Register to read-write to.
  98. * @mask: Mask.
  99. * @val: Value to write.
  100. * @level: Password protected level
  101. */
  102. static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  103. unsigned int mask, unsigned int val, unsigned int level)
  104. {
  105. int ret;
  106. unsigned int data;
  107. ret = tps65217_reg_read(tps, reg, &data);
  108. if (ret) {
  109. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  110. return ret;
  111. }
  112. data &= ~mask;
  113. data |= val & mask;
  114. ret = tps65217_reg_write(tps, reg, data, level);
  115. if (ret)
  116. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  117. return ret;
  118. }
  119. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  120. unsigned int mask, unsigned int val, unsigned int level)
  121. {
  122. return tps65217_update_bits(tps, reg, mask, val, level);
  123. }
  124. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  125. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  126. unsigned int mask, unsigned int level)
  127. {
  128. return tps65217_update_bits(tps, reg, mask, 0, level);
  129. }
  130. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  131. static struct regmap_config tps65217_regmap_config = {
  132. .reg_bits = 8,
  133. .val_bits = 8,
  134. };
  135. static const struct of_device_id tps65217_of_match[] = {
  136. { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
  137. { /* sentinel */ },
  138. };
  139. static int tps65217_probe(struct i2c_client *client,
  140. const struct i2c_device_id *ids)
  141. {
  142. struct tps65217 *tps;
  143. unsigned int version;
  144. unsigned int chip_id = ids->driver_data;
  145. const struct of_device_id *match;
  146. bool status_off = false;
  147. int ret;
  148. if (client->dev.of_node) {
  149. match = of_match_device(tps65217_of_match, &client->dev);
  150. if (!match) {
  151. dev_err(&client->dev,
  152. "Failed to find matching dt id\n");
  153. return -EINVAL;
  154. }
  155. chip_id = (unsigned int)match->data;
  156. status_off = of_property_read_bool(client->dev.of_node,
  157. "ti,pmic-shutdown-controller");
  158. }
  159. if (!chip_id) {
  160. dev_err(&client->dev, "id is null.\n");
  161. return -ENODEV;
  162. }
  163. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  164. if (!tps)
  165. return -ENOMEM;
  166. i2c_set_clientdata(client, tps);
  167. tps->dev = &client->dev;
  168. tps->id = chip_id;
  169. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  170. if (IS_ERR(tps->regmap)) {
  171. ret = PTR_ERR(tps->regmap);
  172. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  173. ret);
  174. return ret;
  175. }
  176. ret = mfd_add_devices(tps->dev, -1, tps65217s,
  177. ARRAY_SIZE(tps65217s), NULL, 0, NULL);
  178. if (ret < 0) {
  179. dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
  180. return ret;
  181. }
  182. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  183. if (ret < 0) {
  184. dev_err(tps->dev, "Failed to read revision register: %d\n",
  185. ret);
  186. return ret;
  187. }
  188. /* Set the PMIC to shutdown on PWR_EN toggle */
  189. if (status_off) {
  190. ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
  191. TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
  192. TPS65217_PROTECT_NONE);
  193. if (ret)
  194. dev_warn(tps->dev, "unable to set the status OFF\n");
  195. }
  196. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  197. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  198. version & TPS65217_CHIPID_REV_MASK);
  199. return 0;
  200. }
  201. static int tps65217_remove(struct i2c_client *client)
  202. {
  203. struct tps65217 *tps = i2c_get_clientdata(client);
  204. mfd_remove_devices(tps->dev);
  205. return 0;
  206. }
  207. static const struct i2c_device_id tps65217_id_table[] = {
  208. {"tps65217", TPS65217},
  209. { /* sentinel */ }
  210. };
  211. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  212. static struct i2c_driver tps65217_driver = {
  213. .driver = {
  214. .name = "tps65217",
  215. .owner = THIS_MODULE,
  216. .of_match_table = of_match_ptr(tps65217_of_match),
  217. },
  218. .id_table = tps65217_id_table,
  219. .probe = tps65217_probe,
  220. .remove = tps65217_remove,
  221. };
  222. static int __init tps65217_init(void)
  223. {
  224. return i2c_add_driver(&tps65217_driver);
  225. }
  226. subsys_initcall(tps65217_init);
  227. static void __exit tps65217_exit(void)
  228. {
  229. i2c_del_driver(&tps65217_driver);
  230. }
  231. module_exit(tps65217_exit);
  232. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  233. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  234. MODULE_LICENSE("GPL v2");