isl6271a-regulator.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * isl6271a-regulator.c
  3. *
  4. * Support for Intersil ISL6271A voltage regulator
  5. *
  6. * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.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 kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #define ISL6271A_VOLTAGE_MIN 850000
  26. #define ISL6271A_VOLTAGE_MAX 1600000
  27. #define ISL6271A_VOLTAGE_STEP 50000
  28. /* PMIC details */
  29. struct isl_pmic {
  30. struct i2c_client *client;
  31. struct regulator_dev *rdev[3];
  32. struct mutex mtx;
  33. };
  34. static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
  35. {
  36. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  37. int idx;
  38. mutex_lock(&pmic->mtx);
  39. idx = i2c_smbus_read_byte(pmic->client);
  40. if (idx < 0)
  41. dev_err(&pmic->client->dev, "Error getting voltage\n");
  42. mutex_unlock(&pmic->mtx);
  43. return idx;
  44. }
  45. static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
  46. unsigned selector)
  47. {
  48. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  49. int err;
  50. mutex_lock(&pmic->mtx);
  51. err = i2c_smbus_write_byte(pmic->client, selector);
  52. if (err < 0)
  53. dev_err(&pmic->client->dev, "Error setting voltage\n");
  54. mutex_unlock(&pmic->mtx);
  55. return err;
  56. }
  57. static struct regulator_ops isl_core_ops = {
  58. .get_voltage_sel = isl6271a_get_voltage_sel,
  59. .set_voltage_sel = isl6271a_set_voltage_sel,
  60. .list_voltage = regulator_list_voltage_linear,
  61. .map_voltage = regulator_map_voltage_linear,
  62. };
  63. static struct regulator_ops isl_fixed_ops = {
  64. .list_voltage = regulator_list_voltage_linear,
  65. };
  66. static const struct regulator_desc isl_rd[] = {
  67. {
  68. .name = "Core Buck",
  69. .id = 0,
  70. .n_voltages = 16,
  71. .ops = &isl_core_ops,
  72. .type = REGULATOR_VOLTAGE,
  73. .owner = THIS_MODULE,
  74. .min_uV = ISL6271A_VOLTAGE_MIN,
  75. .uV_step = ISL6271A_VOLTAGE_STEP,
  76. }, {
  77. .name = "LDO1",
  78. .id = 1,
  79. .n_voltages = 1,
  80. .ops = &isl_fixed_ops,
  81. .type = REGULATOR_VOLTAGE,
  82. .owner = THIS_MODULE,
  83. .min_uV = 1100000,
  84. }, {
  85. .name = "LDO2",
  86. .id = 2,
  87. .n_voltages = 1,
  88. .ops = &isl_fixed_ops,
  89. .type = REGULATOR_VOLTAGE,
  90. .owner = THIS_MODULE,
  91. .min_uV = 1300000,
  92. },
  93. };
  94. static int isl6271a_probe(struct i2c_client *i2c,
  95. const struct i2c_device_id *id)
  96. {
  97. struct regulator_config config = { };
  98. struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
  99. struct isl_pmic *pmic;
  100. int i;
  101. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  102. return -EIO;
  103. pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
  104. if (!pmic)
  105. return -ENOMEM;
  106. pmic->client = i2c;
  107. mutex_init(&pmic->mtx);
  108. for (i = 0; i < 3; i++) {
  109. config.dev = &i2c->dev;
  110. if (i == 0)
  111. config.init_data = init_data;
  112. else
  113. config.init_data = NULL;
  114. config.driver_data = pmic;
  115. pmic->rdev[i] = devm_regulator_register(&i2c->dev, &isl_rd[i],
  116. &config);
  117. if (IS_ERR(pmic->rdev[i])) {
  118. dev_err(&i2c->dev, "failed to register %s\n", id->name);
  119. return PTR_ERR(pmic->rdev[i]);
  120. }
  121. }
  122. i2c_set_clientdata(i2c, pmic);
  123. return 0;
  124. }
  125. static const struct i2c_device_id isl6271a_id[] = {
  126. {.name = "isl6271a", 0 },
  127. { },
  128. };
  129. MODULE_DEVICE_TABLE(i2c, isl6271a_id);
  130. static struct i2c_driver isl6271a_i2c_driver = {
  131. .driver = {
  132. .name = "isl6271a",
  133. },
  134. .probe = isl6271a_probe,
  135. .id_table = isl6271a_id,
  136. };
  137. static int __init isl6271a_init(void)
  138. {
  139. return i2c_add_driver(&isl6271a_i2c_driver);
  140. }
  141. static void __exit isl6271a_cleanup(void)
  142. {
  143. i2c_del_driver(&isl6271a_i2c_driver);
  144. }
  145. subsys_initcall(isl6271a_init);
  146. module_exit(isl6271a_cleanup);
  147. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  148. MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
  149. MODULE_LICENSE("GPL v2");