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/mfd/core.h>
  24. #include <linux/mfd/tps6105x.h>
  25. static struct regmap_config tps6105x_regmap_config = {
  26. .reg_bits = 8,
  27. .val_bits = 8,
  28. .max_register = TPS6105X_REG_3,
  29. };
  30. static int tps6105x_startup(struct tps6105x *tps6105x)
  31. {
  32. int ret;
  33. unsigned int regval;
  34. ret = regmap_read(tps6105x->regmap, TPS6105X_REG_0, &regval);
  35. if (ret)
  36. return ret;
  37. switch (regval >> TPS6105X_REG0_MODE_SHIFT) {
  38. case TPS6105X_REG0_MODE_SHUTDOWN:
  39. dev_info(&tps6105x->client->dev,
  40. "TPS6105x found in SHUTDOWN mode\n");
  41. break;
  42. case TPS6105X_REG0_MODE_TORCH:
  43. dev_info(&tps6105x->client->dev,
  44. "TPS6105x found in TORCH mode\n");
  45. break;
  46. case TPS6105X_REG0_MODE_TORCH_FLASH:
  47. dev_info(&tps6105x->client->dev,
  48. "TPS6105x found in FLASH mode\n");
  49. break;
  50. case TPS6105X_REG0_MODE_VOLTAGE:
  51. dev_info(&tps6105x->client->dev,
  52. "TPS6105x found in VOLTAGE mode\n");
  53. break;
  54. default:
  55. break;
  56. }
  57. return ret;
  58. }
  59. /*
  60. * MFD cells - we always have a GPIO cell and we have one cell
  61. * which is selected operation mode.
  62. */
  63. static struct mfd_cell tps6105x_gpio_cell = {
  64. .name = "tps6105x-gpio",
  65. };
  66. static struct mfd_cell tps6105x_leds_cell = {
  67. .name = "tps6105x-leds",
  68. };
  69. static struct mfd_cell tps6105x_flash_cell = {
  70. .name = "tps6105x-flash",
  71. };
  72. static struct mfd_cell tps6105x_regulator_cell = {
  73. .name = "tps6105x-regulator",
  74. };
  75. static int tps6105x_add_device(struct tps6105x *tps6105x,
  76. struct mfd_cell *cell)
  77. {
  78. cell->platform_data = tps6105x;
  79. cell->pdata_size = sizeof(*tps6105x);
  80. return mfd_add_devices(&tps6105x->client->dev,
  81. PLATFORM_DEVID_AUTO, cell, 1, NULL, 0, NULL);
  82. }
  83. static int tps6105x_probe(struct i2c_client *client,
  84. const struct i2c_device_id *id)
  85. {
  86. struct tps6105x *tps6105x;
  87. struct tps6105x_platform_data *pdata;
  88. int ret;
  89. pdata = dev_get_platdata(&client->dev);
  90. if (!pdata) {
  91. dev_err(&client->dev, "missing platform data\n");
  92. return -ENODEV;
  93. }
  94. tps6105x = devm_kmalloc(&client->dev, sizeof(*tps6105x), GFP_KERNEL);
  95. if (!tps6105x)
  96. return -ENOMEM;
  97. tps6105x->regmap = devm_regmap_init_i2c(client, &tps6105x_regmap_config);
  98. if (IS_ERR(tps6105x->regmap))
  99. return PTR_ERR(tps6105x->regmap);
  100. i2c_set_clientdata(client, tps6105x);
  101. tps6105x->client = client;
  102. tps6105x->pdata = pdata;
  103. ret = tps6105x_startup(tps6105x);
  104. if (ret) {
  105. dev_err(&client->dev, "chip initialization failed\n");
  106. return ret;
  107. }
  108. ret = tps6105x_add_device(tps6105x, &tps6105x_gpio_cell);
  109. if (ret)
  110. return ret;
  111. switch (pdata->mode) {
  112. case TPS6105X_MODE_SHUTDOWN:
  113. dev_info(&client->dev,
  114. "present, not used for anything, only GPIO\n");
  115. break;
  116. case TPS6105X_MODE_TORCH:
  117. ret = tps6105x_add_device(tps6105x, &tps6105x_leds_cell);
  118. break;
  119. case TPS6105X_MODE_TORCH_FLASH:
  120. ret = tps6105x_add_device(tps6105x, &tps6105x_flash_cell);
  121. break;
  122. case TPS6105X_MODE_VOLTAGE:
  123. ret = tps6105x_add_device(tps6105x, &tps6105x_regulator_cell);
  124. break;
  125. default:
  126. dev_warn(&client->dev, "invalid mode: %d\n", pdata->mode);
  127. break;
  128. }
  129. if (ret)
  130. mfd_remove_devices(&client->dev);
  131. return ret;
  132. }
  133. static int tps6105x_remove(struct i2c_client *client)
  134. {
  135. struct tps6105x *tps6105x = i2c_get_clientdata(client);
  136. mfd_remove_devices(&client->dev);
  137. /* Put chip in shutdown mode */
  138. regmap_update_bits(tps6105x->regmap, TPS6105X_REG_0,
  139. TPS6105X_REG0_MODE_MASK,
  140. TPS6105X_MODE_SHUTDOWN << TPS6105X_REG0_MODE_SHIFT);
  141. return 0;
  142. }
  143. static const struct i2c_device_id tps6105x_id[] = {
  144. { "tps61050", 0 },
  145. { "tps61052", 0 },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(i2c, tps6105x_id);
  149. static struct i2c_driver tps6105x_driver = {
  150. .driver = {
  151. .name = "tps6105x",
  152. },
  153. .probe = tps6105x_probe,
  154. .remove = tps6105x_remove,
  155. .id_table = tps6105x_id,
  156. };
  157. static int __init tps6105x_init(void)
  158. {
  159. return i2c_add_driver(&tps6105x_driver);
  160. }
  161. subsys_initcall(tps6105x_init);
  162. static void __exit tps6105x_exit(void)
  163. {
  164. i2c_del_driver(&tps6105x_driver);
  165. }
  166. module_exit(tps6105x_exit);
  167. MODULE_AUTHOR("Linus Walleij");
  168. MODULE_DESCRIPTION("TPS6105x White LED Boost Converter Driver");
  169. MODULE_LICENSE("GPL v2");