tps6105x.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core driver for TPS61050/61052 boost converters, used for while LED
  4. * driving, audio power amplification, white LED flash, and generic
  5. * boost conversion. Additionally it provides a 1-bit GPIO pin (out or in)
  6. * and a flash synchronization pin to synchronize flash events when used as
  7. * flashgun.
  8. *
  9. * Copyright (C) 2011 ST-Ericsson SA
  10. * Written on behalf of Linaro for ST-Ericsson
  11. *
  12. * Author: Linus Walleij <linus.walleij@linaro.org>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/gpio.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/mfd/tps6105x.h>
  24. static struct regmap_config tps6105x_regmap_config = {
  25. .reg_bits = 8,
  26. .val_bits = 8,
  27. .max_register = TPS6105X_REG_3,
  28. };
  29. static int tps6105x_startup(struct tps6105x *tps6105x)
  30. {
  31. int ret;
  32. unsigned int regval;
  33. ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
  34. if (ret)
  35. return ret;
  36. switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
  37. case TPS6105X_REG0_MODE_SHUTDOWN:
  38. dev_info(&tps6105x->client->dev,
  39. "TPS6105x found in SHUTDOWN mode\n");
  40. break;
  41. case TPS6105X_REG0_MODE_TORCH:
  42. dev_info(&tps6105x->client->dev,
  43. "TPS6105x found in TORCH mode\n");
  44. break;
  45. case TPS6105X_REG0_MODE_TORCH_FLASH:
  46. dev_info(&tps6105x->client->dev,
  47. "TPS6105x found in FLASH mode\n");
  48. break;
  49. case TPS6105X_REG0_MODE_VOLTAGE:
  50. dev_info(&tps6105x->client->dev,
  51. "TPS6105x found in VOLTAGE mode\n");
  52. break;
  53. default:
  54. break;
  55. }
  56. return ret;
  57. }
  58. /*
  59. * MFD cells - we always have a GPIO cell and we have one cell
  60. * which is selected operation mode.
  61. */
  62. static struct mfd_cell tps6105x_gpio_cell = {
  63. .name = "tps6105x-gpio",
  64. };
  65. static struct mfd_cell tps6105x_leds_cell = {
  66. .name = "tps6105x-leds",
  67. };
  68. static struct mfd_cell tps6105x_flash_cell = {
  69. .name = "tps6105x-flash",
  70. };
  71. static struct mfd_cell tps6105x_regulator_cell = {
  72. .name = "tps6105x-regulator",
  73. };
  74. static int tps6105x_add_device(struct tps6105x *tps6105x,
  75. struct mfd_cell *cell)
  76. {
  77. cell->platform_data = tps6105x;
  78. cell->pdata_size = sizeof(*tps6105x);
  79. return mfd_add_devices(&tps6105x->client->dev,
  80. PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
  81. }
  82. static struct tps6105x_platform_data *tps6105x_parse_dt(struct device *dev)
  83. {
  84. struct device_node *np = dev->of_node;
  85. struct tps6105x_platform_data *pdata;
  86. struct device_node *child;
  87. if (!np)
  88. return ERR_PTR(-EINVAL);
  89. if (of_get_available_child_count(np) > 1) {
  90. dev_err(dev, "cannot support multiple operational modes");
  91. return ERR_PTR(-EINVAL);
  92. }
  93. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  94. if (!pdata)
  95. return ERR_PTR(-ENOMEM);
  96. pdata->mode = TPS6105X_MODE_SHUTDOWN;
  97. for_each_available_child_of_node(np, child) {
  98. if (child->name && !of_node_cmp(child->name, "regulator"))
  99. pdata->mode = TPS6105X_MODE_VOLTAGE;
  100. else if (child->name && !of_node_cmp(child->name, "led"))
  101. pdata->mode = TPS6105X_MODE_TORCH;
  102. }
  103. return pdata;
  104. }
  105. static int tps6105x_probe(struct i2c_client *client,
  106. const struct i2c_device_id *id)
  107. {
  108. struct tps6105x *tps6105x;
  109. struct tps6105x_platform_data *pdata;
  110. int ret;
  111. pdata = dev_get_platdata(&client->dev);
  112. if (!pdata)
  113. pdata = tps6105x_parse_dt(&client->dev);
  114. if (IS_ERR(pdata)) {
  115. dev_err(&client->dev, "No platform data or DT found");
  116. return PTR_ERR(pdata);
  117. }
  118. tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
  119. if (!tps6105x)
  120. return -ENOMEM;
  121. tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
  122. if (IS_ERR(tps6105x->regmap))
  123. return PTR_ERR(tps6105x->regmap);
  124. i2c_set_clientdata(client, tps6105x);
  125. tps6105x->client = client;
  126. tps6105x->pdata = pdata;
  127. ret = tps6105x_startup(tps6105x);
  128. if (ret) {
  129. dev_err(&client->dev, "chip initialization failed\n");
  130. return ret;
  131. }
  132. ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
  133. if (ret)
  134. return ret;
  135. switch (pdata->mode) {
  136. case TPS6105X_MODE_SHUTDOWN:
  137. dev_info(&client->dev,
  138. "present, not used for anything, only GPIO\n");
  139. break;
  140. case TPS6105X_MODE_TORCH:
  141. ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
  142. break;
  143. case TPS6105X_MODE_TORCH_FLASH:
  144. ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
  145. break;
  146. case TPS6105X_MODE_VOLTAGE:
  147. ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
  148. break;
  149. default:
  150. dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
  151. break;
  152. }
  153. if (ret)
  154. mfd_remove_devices(&client->dev);
  155. return ret;
  156. }
  157. static int tps6105x_remove(struct i2c_client *client)
  158. {
  159. struct tps6105x *tps6105x = i2c_get_clientdata(client);
  160. mfd_remove_devices(&client->dev);
  161. /* Put chip in shutdown mode */
  162. regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
  163. TPS6105X_REG0_MODE_MASK,
  164. TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
  165. return 0;
  166. }
  167. static const struct i2c_device_id tps6105x_id[] = {
  168. { "tps61050", 0 },
  169. { "tps61052", 0 },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(i2c, tps6105x_id);
  173. static const struct of_device_id tps6105x_of_match[] = {
  174. { .compatible = "ti,tps61050" },
  175. { .compatible = "ti,tps61052" },
  176. { },
  177. };
  178. MODULE_DEVICE_TABLE(of, tps6105x_of_match);
  179. static struct i2c_driver tps6105x_driver = {
  180. .driver = {
  181. .name = "tps6105x",
  182. .of_match_table = tps6105x_of_match,
  183. },
  184. .probe = tps6105x_probe,
  185. .remove = tps6105x_remove,
  186. .id_table = tps6105x_id,
  187. };
  188. static int __init tps6105x_init(void)
  189. {
  190. return i2c_add_driver(&tps6105x_driver);
  191. }
  192. subsys_initcall(tps6105x_init);
  193. static void __exit tps6105x_exit(void)
  194. {
  195. i2c_del_driver(&tps6105x_driver);
  196. }
  197. module_exit(tps6105x_exit);
  198. MODULE_AUTHOR("Linus Walleij");
  199. MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
  200. MODULE_LICENSE("GPL v2");