tps6105x.c 4.7 KB

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