w1_bq27000.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * drivers/w1/slaves/w1_bq27000.c
  3. *
  4. * Copyright (C) 2007 Texas Instruments, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/types.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mutex.h>
  17. #include <linux/power/bq27xxx_battery.h>
  18. #include "../w1.h"
  19. #include "../w1_int.h"
  20. #include "../w1_family.h"
  21. #define HDQ_CMD_READ (0)
  22. #define HDQ_CMD_WRITE (1<<7)
  23. static int F_ID;
  24. static int w1_bq27000_read(struct device *dev, unsigned int reg)
  25. {
  26. u8 val;
  27. struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev);
  28. mutex_lock(&sl->master->bus_mutex);
  29. w1_write_8(sl->master, HDQ_CMD_READ | reg);
  30. val = w1_read_8(sl->master);
  31. mutex_unlock(&sl->master->bus_mutex);
  32. return val;
  33. }
  34. static struct bq27xxx_platform_data bq27000_battery_info = {
  35. .read = w1_bq27000_read,
  36. .name = "bq27000-battery",
  37. .chip = BQ27000,
  38. };
  39. static int w1_bq27000_add_slave(struct w1_slave *sl)
  40. {
  41. int ret;
  42. struct platform_device *pdev;
  43. pdev = platform_device_alloc("bq27000-battery", -1);
  44. if (!pdev) {
  45. ret = -ENOMEM;
  46. return ret;
  47. }
  48. ret = platform_device_add_data(pdev,
  49. &bq27000_battery_info,
  50. sizeof(bq27000_battery_info));
  51. if (ret)
  52. goto pdev_add_failed;
  53. pdev->dev.parent = &sl->dev;
  54. ret = platform_device_add(pdev);
  55. if (ret)
  56. goto pdev_add_failed;
  57. dev_set_drvdata(&sl->dev, pdev);
  58. goto success;
  59. pdev_add_failed:
  60. platform_device_put(pdev);
  61. success:
  62. return ret;
  63. }
  64. static void w1_bq27000_remove_slave(struct w1_slave *sl)
  65. {
  66. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  67. platform_device_unregister(pdev);
  68. }
  69. static struct w1_family_ops w1_bq27000_fops = {
  70. .add_slave = w1_bq27000_add_slave,
  71. .remove_slave = w1_bq27000_remove_slave,
  72. };
  73. static struct w1_family w1_bq27000_family = {
  74. .fid = W1_FAMILY_BQ27000,
  75. .fops = &w1_bq27000_fops,
  76. };
  77. static int __init w1_bq27000_init(void)
  78. {
  79. if (F_ID)
  80. w1_bq27000_family.fid = F_ID;
  81. return w1_register_family(&w1_bq27000_family);
  82. }
  83. static void __exit w1_bq27000_exit(void)
  84. {
  85. w1_unregister_family(&w1_bq27000_family);
  86. }
  87. module_init(w1_bq27000_init);
  88. module_exit(w1_bq27000_exit);
  89. module_param(F_ID, int, S_IRUSR);
  90. MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device");
  91. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_BQ27000));
  92. MODULE_LICENSE("GPL");
  93. MODULE_AUTHOR("Texas Instruments Ltd");
  94. MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip");