blob: caafb1722783ce66c615deb32d0a744a8f07f503 [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
NeilBrown9f3519d2012-02-15 18:21:42 +010029static int w1_bq27000_read(struct device *dev, unsigned int reg)
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080030{
31 u8 val;
NeilBrown9f3519d2012-02-15 18:21:42 +010032 struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080033
NeilBrownb02f8be2012-05-18 15:59:52 +100034 mutex_lock(&sl->master->bus_mutex);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080035 w1_write_8(sl->master, HDQ_CMD_READ | reg);
36 val = w1_read_8(sl->master);
NeilBrownb02f8be2012-05-18 15:59:52 +100037 mutex_unlock(&sl->master->bus_mutex);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080038
39 return val;
40}
NeilBrown9f3519d2012-02-15 18:21:42 +010041
42static struct bq27000_platform_data bq27000_battery_info = {
43 .read = w1_bq27000_read,
44 .name = "bq27000-battery",
45};
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080046
47static int w1_bq27000_add_slave(struct w1_slave *sl)
48{
49 int ret;
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080050 struct platform_device *pdev;
51
NeilBrown9f3519d2012-02-15 18:21:42 +010052 pdev = platform_device_alloc("bq27000-battery", -1);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080053 if (!pdev) {
54 ret = -ENOMEM;
55 return ret;
56 }
NeilBrown9f3519d2012-02-15 18:21:42 +010057 ret = platform_device_add_data(pdev,
58 &bq27000_battery_info,
59 sizeof(bq27000_battery_info));
Wei Yongjun0ece1bb2013-04-30 15:28:35 -070060 if (ret)
61 goto pdev_add_failed;
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080062 pdev->dev.parent = &sl->dev;
63
64 ret = platform_device_add(pdev);
65 if (ret)
66 goto pdev_add_failed;
67
68 dev_set_drvdata(&sl->dev, pdev);
69
70 goto success;
71
72pdev_add_failed:
Wei Yongjun0ece1bb2013-04-30 15:28:35 -070073 platform_device_put(pdev);
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080074success:
75 return ret;
76}
77
78static void w1_bq27000_remove_slave(struct w1_slave *sl)
79{
80 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
81
82 platform_device_unregister(pdev);
83}
84
85static struct w1_family_ops w1_bq27000_fops = {
86 .add_slave = w1_bq27000_add_slave,
87 .remove_slave = w1_bq27000_remove_slave,
88};
89
90static struct w1_family w1_bq27000_family = {
NeilBrown4b7e4f82014-11-19 14:04:20 +110091 .fid = W1_FAMILY_BQ27000,
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -080092 .fops = &w1_bq27000_fops,
93};
94
95static int __init w1_bq27000_init(void)
96{
97 if (F_ID)
98 w1_bq27000_family.fid = F_ID;
99
100 return w1_register_family(&w1_bq27000_family);
101}
102
103static void __exit w1_bq27000_exit(void)
104{
105 w1_unregister_family(&w1_bq27000_family);
106}
107
108
109module_init(w1_bq27000_init);
110module_exit(w1_bq27000_exit);
111
112module_param(F_ID, int, S_IRUSR);
113MODULE_PARM_DESC(F_ID, "1-wire slave FID for BQ device");
NeilBrown4b7e4f82014-11-19 14:04:20 +1100114MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_BQ27000));
Madhusudhan Chikkaturecfbc6192008-11-12 13:27:11 -0800115MODULE_LICENSE("GPL");
116MODULE_AUTHOR("Texas Instruments Ltd");
117MODULE_DESCRIPTION("HDQ/1-wire slave driver bq27000 battery monitor chip");