da9052-i2c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * I2C access for DA9052 PMICs.
  3. *
  4. * Copyright(c) 2011 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/input.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/i2c.h>
  19. #include <linux/err.h>
  20. #include <linux/mfd/da9052/da9052.h>
  21. #include <linux/mfd/da9052/reg.h>
  22. static int da9052_i2c_enable_multiwrite(struct da9052 *da9052)
  23. {
  24. int reg_val, ret;
  25. ret = regmap_read(da9052->regmap, DA9052_CONTROL_B_REG, &reg_val);
  26. if (ret < 0)
  27. return ret;
  28. if (reg_val & DA9052_CONTROL_B_WRITEMODE) {
  29. reg_val &= ~DA9052_CONTROL_B_WRITEMODE;
  30. ret = regmap_write(da9052->regmap, DA9052_CONTROL_B_REG,
  31. reg_val);
  32. if (ret < 0)
  33. return ret;
  34. }
  35. return 0;
  36. }
  37. static int __devinit da9052_i2c_probe(struct i2c_client *client,
  38. const struct i2c_device_id *id)
  39. {
  40. struct da9052 *da9052;
  41. int ret;
  42. da9052 = kzalloc(sizeof(struct da9052), GFP_KERNEL);
  43. if (!da9052)
  44. return -ENOMEM;
  45. if (!i2c_check_functionality(client->adapter,
  46. I2C_FUNC_SMBUS_BYTE_DATA)) {
  47. dev_info(&client->dev, "Error in %s:i2c_check_functionality\n",
  48. __func__);
  49. ret = -ENODEV;
  50. goto err;
  51. }
  52. da9052->dev = &client->dev;
  53. da9052->chip_irq = client->irq;
  54. i2c_set_clientdata(client, da9052);
  55. da9052->regmap = regmap_init_i2c(client, &da9052_regmap_config);
  56. if (IS_ERR(da9052->regmap)) {
  57. ret = PTR_ERR(da9052->regmap);
  58. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  59. ret);
  60. goto err;
  61. }
  62. ret = da9052_i2c_enable_multiwrite(da9052);
  63. if (ret < 0)
  64. goto err_regmap;
  65. ret = da9052_device_init(da9052, id->driver_data);
  66. if (ret != 0)
  67. goto err_regmap;
  68. return 0;
  69. err_regmap:
  70. regmap_exit(da9052->regmap);
  71. err:
  72. kfree(da9052);
  73. return ret;
  74. }
  75. static int __devexit da9052_i2c_remove(struct i2c_client *client)
  76. {
  77. struct da9052 *da9052 = i2c_get_clientdata(client);
  78. da9052_device_exit(da9052);
  79. regmap_exit(da9052->regmap);
  80. kfree(da9052);
  81. return 0;
  82. }
  83. static struct i2c_device_id da9052_i2c_id[] = {
  84. {"da9052", DA9052},
  85. {"da9053-aa", DA9053_AA},
  86. {"da9053-ba", DA9053_BA},
  87. {"da9053-bb", DA9053_BB},
  88. {}
  89. };
  90. static struct i2c_driver da9052_i2c_driver = {
  91. .probe = da9052_i2c_probe,
  92. .remove = __devexit_p(da9052_i2c_remove),
  93. .id_table = da9052_i2c_id,
  94. .driver = {
  95. .name = "da9052",
  96. .owner = THIS_MODULE,
  97. },
  98. };
  99. static int __init da9052_i2c_init(void)
  100. {
  101. int ret;
  102. ret = i2c_add_driver(&da9052_i2c_driver);
  103. if (ret != 0) {
  104. pr_err("DA9052 I2C registration failed %d\n", ret);
  105. return ret;
  106. }
  107. return 0;
  108. }
  109. subsys_initcall(da9052_i2c_init);
  110. static void __exit da9052_i2c_exit(void)
  111. {
  112. i2c_del_driver(&da9052_i2c_driver);
  113. }
  114. module_exit(da9052_i2c_exit);
  115. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  116. MODULE_DESCRIPTION("I2C driver for Dialog DA9052 PMIC");
  117. MODULE_LICENSE("GPL");