w1_bq27000.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/bq27x00_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->mutex);
  29. w1_write_8(sl->master, HDQ_CMD_READ | reg);
  30. val = w1_read_8(sl->master);
  31. mutex_unlock(&sl->master->mutex);
  32. return val;
  33. }
  34. static struct bq27000_platform_data bq27000_battery_info = {
  35. .read = w1_bq27000_read,
  36. .name = "bq27000-battery",
  37. };
  38. static int w1_bq27000_add_slave(struct w1_slave *sl)
  39. {
  40. int ret;
  41. struct platform_device *pdev;
  42. pdev = platform_device_alloc("bq27000-battery", -1);
  43. if (!pdev) {
  44. ret = -ENOMEM;
  45. return ret;
  46. }
  47. ret = platform_device_add_data(pdev,
  48. &bq27000_battery_info,
  49. sizeof(bq27000_battery_info));
  50. pdev->dev.parent = &sl->dev;
  51. ret = platform_device_add(pdev);
  52. if (ret)
  53. goto pdev_add_failed;
  54. dev_set_drvdata(&sl->dev, pdev);
  55. goto success;
  56. pdev_add_failed:
  57. platform_device_unregister(pdev);
  58. success:
  59. return ret;
  60. }
  61. static void w1_bq27000_remove_slave(struct w1_slave *sl)
  62. {
  63. struct platform_device *pdev = dev_get_drvdata(&sl->dev);
  64. platform_device_unregister(pdev);
  65. }
  66. static struct w1_family_ops w1_bq27000_fops = {
  67. .add_slave = w1_bq27000_add_slave,
  68. .remove_slave = w1_bq27000_remove_slave,
  69. };
  70. static struct w1_family w1_bq27000_family = {
  71. .fid = 1,
  72. .fops = &w1_bq27000_fops,
  73. };
  74. static int __init w1_bq27000_init(void)
  75. {
  76. if (F_ID)
  77. w1_bq27000_family.fid = F_ID;
  78. return w1_register_family(&w1_bq27000_family);
  79. }
  80. static void __exit w1_bq27000_exit(void)
  81. {
  82. w1_unregister_family(&w1_bq27000_family);
  83. }
  84. module_init(w1_bq27000_init);
  85. module_exit(w1_bq27000_exit);
  86. module_param(F_ID, int, S_IRUSR);
  87. MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device");
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Texas Instruments Ltd");
  90. MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip");