isl6271a-regulator.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 mutex mtx;
  32. };
  33. static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
  34. {
  35. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  36. int idx;
  37. mutex_lock(&pmic->mtx);
  38. idx = i2c_smbus_read_byte(pmic->client);
  39. if (idx < 0)
  40. dev_err(&pmic->client->dev, "Error getting voltage\n");
  41. mutex_unlock(&pmic->mtx);
  42. return idx;
  43. }
  44. static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
  45. unsigned selector)
  46. {
  47. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  48. int err;
  49. mutex_lock(&pmic->mtx);
  50. err = i2c_smbus_write_byte(pmic->client, selector);
  51. if (err < 0)
  52. dev_err(&pmic->client->dev, "Error setting voltage\n");
  53. mutex_unlock(&pmic->mtx);
  54. return err;
  55. }
  56. static const struct regulator_ops isl_core_ops = {
  57. .get_voltage_sel = isl6271a_get_voltage_sel,
  58. .set_voltage_sel = isl6271a_set_voltage_sel,
  59. .list_voltage = regulator_list_voltage_linear,
  60. .map_voltage = regulator_map_voltage_linear,
  61. };
  62. static const struct regulator_ops isl_fixed_ops = {
  63. .list_voltage = regulator_list_voltage_linear,
  64. };
  65. static const struct regulator_desc isl_rd[] = {
  66. {
  67. .name = "Core Buck",
  68. .id = 0,
  69. .n_voltages = 16,
  70. .ops = &isl_core_ops,
  71. .type = REGULATOR_VOLTAGE,
  72. .owner = THIS_MODULE,
  73. .min_uV = ISL6271A_VOLTAGE_MIN,
  74. .uV_step = ISL6271A_VOLTAGE_STEP,
  75. }, {
  76. .name = "LDO1",
  77. .id = 1,
  78. .n_voltages = 1,
  79. .ops = &isl_fixed_ops,
  80. .type = REGULATOR_VOLTAGE,
  81. .owner = THIS_MODULE,
  82. .min_uV = 1100000,
  83. }, {
  84. .name = "LDO2",
  85. .id = 2,
  86. .n_voltages = 1,
  87. .ops = &isl_fixed_ops,
  88. .type = REGULATOR_VOLTAGE,
  89. .owner = THIS_MODULE,
  90. .min_uV = 1300000,
  91. },
  92. };
  93. static int isl6271a_probe(struct i2c_client *i2c,
  94. const struct i2c_device_id *id)
  95. {
  96. struct regulator_dev *rdev;
  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. rdev = devm_regulator_register(&i2c->dev, &isl_rd[i], &config);
  116. if (IS_ERR(rdev)) {
  117. dev_err(&i2c->dev, "failed to register %s\n", id->name);
  118. return PTR_ERR(rdev);
  119. }
  120. }
  121. i2c_set_clientdata(i2c, pmic);
  122. return 0;
  123. }
  124. static const struct i2c_device_id isl6271a_id[] = {
  125. {.name = "isl6271a", 0 },
  126. { },
  127. };
  128. MODULE_DEVICE_TABLE(i2c, isl6271a_id);
  129. static struct i2c_driver isl6271a_i2c_driver = {
  130. .driver = {
  131. .name = "isl6271a",
  132. },
  133. .probe = isl6271a_probe,
  134. .id_table = isl6271a_id,
  135. };
  136. static int __init isl6271a_init(void)
  137. {
  138. return i2c_add_driver(&isl6271a_i2c_driver);
  139. }
  140. static void __exit isl6271a_cleanup(void)
  141. {
  142. i2c_del_driver(&isl6271a_i2c_driver);
  143. }
  144. subsys_initcall(isl6271a_init);
  145. module_exit(isl6271a_cleanup);
  146. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  147. MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
  148. MODULE_LICENSE("GPL v2");