tps65217.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/mfd/core.h>
  27. #include <linux/mfd/tps65217.h>
  28. /**
  29. * tps65217_reg_read: Read a single tps65217 register.
  30. *
  31. * @tps: Device to read from.
  32. * @reg: Register to read.
  33. * @val: Contians the value
  34. */
  35. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  36. unsigned int *val)
  37. {
  38. return regmap_read(tps->regmap, reg, val);
  39. }
  40. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  41. /**
  42. * tps65217_reg_write: Write a single tps65217 register.
  43. *
  44. * @tps65217: Device to write to.
  45. * @reg: Register to write to.
  46. * @val: Value to write.
  47. * @level: Password protected level
  48. */
  49. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  50. unsigned int val, unsigned int level)
  51. {
  52. int ret;
  53. unsigned int xor_reg_val;
  54. switch (level) {
  55. case TPS65217_PROTECT_NONE:
  56. return regmap_write(tps->regmap, reg, val);
  57. case TPS65217_PROTECT_L1:
  58. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  59. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  60. xor_reg_val);
  61. if (ret < 0)
  62. return ret;
  63. return regmap_write(tps->regmap, reg, val);
  64. case TPS65217_PROTECT_L2:
  65. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  66. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  67. xor_reg_val);
  68. if (ret < 0)
  69. return ret;
  70. ret = regmap_write(tps->regmap, reg, val);
  71. if (ret < 0)
  72. return ret;
  73. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  74. xor_reg_val);
  75. if (ret < 0)
  76. return ret;
  77. return regmap_write(tps->regmap, reg, val);
  78. default:
  79. return -EINVAL;
  80. }
  81. }
  82. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  83. /**
  84. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  85. *
  86. * @tps65217: Device to write to.
  87. * @reg: Register to read-write to.
  88. * @mask: Mask.
  89. * @val: Value to write.
  90. * @level: Password protected level
  91. */
  92. int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  93. unsigned int mask, unsigned int val, unsigned int level)
  94. {
  95. int ret;
  96. unsigned int data;
  97. ret = tps65217_reg_read(tps, reg, &data);
  98. if (ret) {
  99. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  100. return ret;
  101. }
  102. data &= ~mask;
  103. data |= val & mask;
  104. ret = tps65217_reg_write(tps, reg, data, level);
  105. if (ret)
  106. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  107. return ret;
  108. }
  109. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  110. unsigned int mask, unsigned int val, unsigned int level)
  111. {
  112. return tps65217_update_bits(tps, reg, mask, val, level);
  113. }
  114. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  115. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  116. unsigned int mask, unsigned int level)
  117. {
  118. return tps65217_update_bits(tps, reg, mask, 0, level);
  119. }
  120. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  121. static struct regmap_config tps65217_regmap_config = {
  122. .reg_bits = 8,
  123. .val_bits = 8,
  124. };
  125. static int __devinit tps65217_probe(struct i2c_client *client,
  126. const struct i2c_device_id *ids)
  127. {
  128. struct tps65217 *tps;
  129. struct tps65217_board *pdata = client->dev.platform_data;
  130. int i, ret;
  131. unsigned int version;
  132. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  133. if (!tps)
  134. return -ENOMEM;
  135. tps->pdata = pdata;
  136. tps->regmap = regmap_init_i2c(client, &tps65217_regmap_config);
  137. if (IS_ERR(tps->regmap)) {
  138. ret = PTR_ERR(tps->regmap);
  139. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  140. ret);
  141. return ret;
  142. }
  143. i2c_set_clientdata(client, tps);
  144. tps->dev = &client->dev;
  145. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  146. if (ret < 0) {
  147. dev_err(tps->dev, "Failed to read revision"
  148. " register: %d\n", ret);
  149. goto err_regmap;
  150. }
  151. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  152. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  153. version & TPS65217_CHIPID_REV_MASK);
  154. for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
  155. struct platform_device *pdev;
  156. pdev = platform_device_alloc("tps65217-pmic", i);
  157. if (!pdev) {
  158. dev_err(tps->dev, "Cannot create regulator %d\n", i);
  159. continue;
  160. }
  161. pdev->dev.parent = tps->dev;
  162. platform_device_add_data(pdev, &pdata->tps65217_init_data[i],
  163. sizeof(pdata->tps65217_init_data[i]));
  164. tps->regulator_pdev[i] = pdev;
  165. platform_device_add(pdev);
  166. }
  167. return 0;
  168. err_regmap:
  169. regmap_exit(tps->regmap);
  170. return ret;
  171. }
  172. static int __devexit tps65217_remove(struct i2c_client *client)
  173. {
  174. struct tps65217 *tps = i2c_get_clientdata(client);
  175. int i;
  176. for (i = 0; i < TPS65217_NUM_REGULATOR; i++)
  177. platform_device_unregister(tps->regulator_pdev[i]);
  178. regmap_exit(tps->regmap);
  179. return 0;
  180. }
  181. static const struct i2c_device_id tps65217_id_table[] = {
  182. {"tps65217", 0xF0},
  183. {/* end of list */}
  184. };
  185. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  186. static struct i2c_driver tps65217_driver = {
  187. .driver = {
  188. .name = "tps65217",
  189. },
  190. .id_table = tps65217_id_table,
  191. .probe = tps65217_probe,
  192. .remove = __devexit_p(tps65217_remove),
  193. };
  194. static int __init tps65217_init(void)
  195. {
  196. return i2c_add_driver(&tps65217_driver);
  197. }
  198. subsys_initcall(tps65217_init);
  199. static void __exit tps65217_exit(void)
  200. {
  201. i2c_del_driver(&tps65217_driver);
  202. }
  203. module_exit(tps65217_exit);
  204. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  205. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  206. MODULE_LICENSE("GPL v2");