Linux 3.11-rc3
[cris-mirror.git] / drivers / w1 / slaves / w1_bq27000.c
blobafbefed5f2c988fe98db5ded3c13c72479dcdf90
1 /*
2 * drivers/w1/slaves/w1_bq27000.c
4 * Copyright (C) 2007 Texas Instruments, Inc.
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/types.h>
16 #include <linux/platform_device.h>
17 #include <linux/mutex.h>
18 #include <linux/power/bq27x00_battery.h>
20 #include "../w1.h"
21 #include "../w1_int.h"
22 #include "../w1_family.h"
24 #define HDQ_CMD_READ (0)
25 #define HDQ_CMD_WRITE (1<<7)
27 static int F_ID;
29 static int w1_bq27000_read(struct device *dev, unsigned int reg)
31 u8 val;
32 struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev);
34 mutex_lock(&sl->master->bus_mutex);
35 w1_write_8(sl->master, HDQ_CMD_READ | reg);
36 val = w1_read_8(sl->master);
37 mutex_unlock(&sl->master->bus_mutex);
39 return val;
42 static struct bq27000_platform_data bq27000_battery_info = {
43 .read = w1_bq27000_read,
44 .name = "bq27000-battery",
47 static int w1_bq27000_add_slave(struct w1_slave *sl)
49 int ret;
50 struct platform_device *pdev;
52 pdev = platform_device_alloc("bq27000-battery", -1);
53 if (!pdev) {
54 ret = -ENOMEM;
55 return ret;
57 ret = platform_device_add_data(pdev,
58 &bq27000_battery_info,
59 sizeof(bq27000_battery_info));
60 if (ret)
61 goto pdev_add_failed;
62 pdev->dev.parent = &sl->dev;
64 ret = platform_device_add(pdev);
65 if (ret)
66 goto pdev_add_failed;
68 dev_set_drvdata(&sl->dev, pdev);
70 goto success;
72 pdev_add_failed:
73 platform_device_put(pdev);
74 success:
75 return ret;
78 static void w1_bq27000_remove_slave(struct w1_slave *sl)
80 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
82 platform_device_unregister(pdev);
85 static struct w1_family_ops w1_bq27000_fops = {
86 .add_slave = w1_bq27000_add_slave,
87 .remove_slave = w1_bq27000_remove_slave,
90 static struct w1_family w1_bq27000_family = {
91 .fid = 1,
92 .fops = &w1_bq27000_fops,
95 static int __init w1_bq27000_init(void)
97 if (F_ID)
98 w1_bq27000_family.fid = F_ID;
100 return w1_register_family(&w1_bq27000_family);
103 static void __exit w1_bq27000_exit(void)
105 w1_unregister_family(&w1_bq27000_family);
109 module_init(w1_bq27000_init);
110 module_exit(w1_bq27000_exit);
112 module_param(F_ID, int, S_IRUSR);
113 MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device");
115 MODULE_LICENSE("GPL");
116 MODULE_AUTHOR("Texas Instruments Ltd");
117 MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip");