blob: 8f10fd2630a0b0ca72194c14f40f2cf20fe3ff05 [file] [log] [blame]
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -08001/*
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
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>
NeilBrown9f3519d2012-02-15 18:21:42 +010018#include <linux/power/bq27x00_battery.h>
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080019
20#include "../w1.h"
21#include "../w1_int.h"
22#include "../w1_family.h"
23
24#define HDQ_CMD_READ (0)
25#define HDQ_CMD_WRITE (1<<7)
26
27static int F_ID;
28
29void w1_bq27000_write(struct device *dev, u8 buf, u8 reg)
30{
31 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
32
33 if (!dev) {
34 pr_info("Could not obtain slave dev ptr\n");
35 return;
36 }
37
38 w1_write_8(sl->master, HDQ_CMD_WRITE | reg);
39 w1_write_8(sl->master, buf);
40}
41EXPORT_SYMBOL(w1_bq27000_write);
42
NeilBrown9f3519d2012-02-15 18:21:42 +010043static int w1_bq27000_read(struct device *dev, unsigned int reg)
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080044{
45 u8 val;
NeilBrown9f3519d2012-02-15 18:21:42 +010046 struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080047
48 if (!dev)
49 return 0;
50
51 w1_write_8(sl->master, HDQ_CMD_READ | reg);
52 val = w1_read_8(sl->master);
53
54 return val;
55}
NeilBrown9f3519d2012-02-15 18:21:42 +010056
57static struct bq27000_platform_data bq27000_battery_info = {
58 .read = w1_bq27000_read,
59 .name = "bq27000-battery",
60};
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080061
62static int w1_bq27000_add_slave(struct w1_slave *sl)
63{
64 int ret;
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080065 struct platform_device *pdev;
66
NeilBrown9f3519d2012-02-15 18:21:42 +010067 pdev = platform_device_alloc("bq27000-battery", -1);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080068 if (!pdev) {
69 ret = -ENOMEM;
70 return ret;
71 }
NeilBrown9f3519d2012-02-15 18:21:42 +010072 ret = platform_device_add_data(pdev,
73 &bq27000_battery_info,
74 sizeof(bq27000_battery_info));
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080075 pdev->dev.parent = &sl->dev;
76
77 ret = platform_device_add(pdev);
78 if (ret)
79 goto pdev_add_failed;
80
81 dev_set_drvdata(&sl->dev, pdev);
82
83 goto success;
84
85pdev_add_failed:
86 platform_device_unregister(pdev);
87success:
88 return ret;
89}
90
91static void w1_bq27000_remove_slave(struct w1_slave *sl)
92{
93 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
94
95 platform_device_unregister(pdev);
96}
97
98static struct w1_family_ops w1_bq27000_fops = {
99 .add_slave = w1_bq27000_add_slave,
100 .remove_slave = w1_bq27000_remove_slave,
101};
102
103static struct w1_family w1_bq27000_family = {
104 .fid = 1,
105 .fops = &w1_bq27000_fops,
106};
107
108static int __init w1_bq27000_init(void)
109{
110 if (F_ID)
111 w1_bq27000_family.fid = F_ID;
112
113 return w1_register_family(&w1_bq27000_family);
114}
115
116static void __exit w1_bq27000_exit(void)
117{
118 w1_unregister_family(&w1_bq27000_family);
119}
120
121
122module_init(w1_bq27000_init);
123module_exit(w1_bq27000_exit);
124
125module_param(F_ID, int, S_IRUSR);
126MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device");
127
128MODULE_LICENSE("GPL");
129MODULE_AUTHOR("Texas Instruments Ltd");
130MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip");