blob: 0cd7a27b5d6b508d05bca2a7ace486308f412012 [file] [log] [blame]
Clifton Barnes275ac742011-05-26 16:26:04 -07001/*
2 * 1-Wire implementation for the ds2780 chip
3 *
4 * Copyright (C) 2010 Indesign, LLC
5 *
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
7 *
8 * Based on w1-ds2760 driver
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/types.h>
20#include <linux/platform_device.h>
21#include <linux/mutex.h>
22#include <linux/idr.h>
23
24#include "../w1.h"
25#include "../w1_int.h"
26#include "../w1_family.h"
27#include "w1_ds2780.h"
28
Clifton Barnes9fe678f2011-11-02 13:39:52 -070029static int w1_ds2780_do_io(struct device *dev, char *buf, int addr,
30 size_t count, int io)
Clifton Barnes275ac742011-05-26 16:26:04 -070031{
32 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
33
Clifton Barnes9fe678f2011-11-02 13:39:52 -070034 if (addr > DS2780_DATA_SIZE || addr < 0)
35 return 0;
Clifton Barnes275ac742011-05-26 16:26:04 -070036
Clifton Barnes275ac742011-05-26 16:26:04 -070037 count = min_t(int, count, DS2780_DATA_SIZE - addr);
38
39 if (w1_reset_select_slave(sl) == 0) {
40 if (io) {
41 w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
42 w1_write_8(sl->master, addr);
43 w1_write_block(sl->master, buf, count);
Clifton Barnes275ac742011-05-26 16:26:04 -070044 } else {
45 w1_write_8(sl->master, W1_DS2780_READ_DATA);
46 w1_write_8(sl->master, addr);
47 count = w1_read_block(sl->master, buf, count);
48 }
49 }
50
Clifton Barnes275ac742011-05-26 16:26:04 -070051 return count;
52}
Clifton Barnes9fe678f2011-11-02 13:39:52 -070053
54int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
55 int io)
56{
57 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
58 int ret;
59
60 if (!dev)
61 return -ENODEV;
62
NeilBrownb02f8be2012-05-18 15:59:52 +100063 mutex_lock(&sl->master->bus_mutex);
Clifton Barnes9fe678f2011-11-02 13:39:52 -070064
65 ret = w1_ds2780_do_io(dev, buf, addr, count, io);
66
NeilBrownb02f8be2012-05-18 15:59:52 +100067 mutex_unlock(&sl->master->bus_mutex);
Clifton Barnes9fe678f2011-11-02 13:39:52 -070068
69 return ret;
70}
Clifton Barnes275ac742011-05-26 16:26:04 -070071EXPORT_SYMBOL(w1_ds2780_io);
72
73int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
74{
75 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
76
77 if (!dev)
78 return -EINVAL;
79
NeilBrownb02f8be2012-05-18 15:59:52 +100080 mutex_lock(&sl->master->bus_mutex);
Clifton Barnes275ac742011-05-26 16:26:04 -070081
82 if (w1_reset_select_slave(sl) == 0) {
83 w1_write_8(sl->master, cmd);
84 w1_write_8(sl->master, addr);
85 }
86
NeilBrownb02f8be2012-05-18 15:59:52 +100087 mutex_unlock(&sl->master->bus_mutex);
Clifton Barnes275ac742011-05-26 16:26:04 -070088 return 0;
89}
90EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
91
92static ssize_t w1_ds2780_read_bin(struct file *filp,
93 struct kobject *kobj,
94 struct bin_attribute *bin_attr,
95 char *buf, loff_t off, size_t count)
96{
97 struct device *dev = container_of(kobj, struct device, kobj);
98 return w1_ds2780_io(dev, buf, off, count, 0);
99}
100
101static struct bin_attribute w1_ds2780_bin_attr = {
102 .attr = {
103 .name = "w1_slave",
104 .mode = S_IRUGO,
105 },
106 .size = DS2780_DATA_SIZE,
107 .read = w1_ds2780_read_bin,
108};
109
Jonathan Cameron3e542812011-11-02 13:39:43 -0700110static DEFINE_IDA(bat_ida);
Clifton Barnes275ac742011-05-26 16:26:04 -0700111
112static int w1_ds2780_add_slave(struct w1_slave *sl)
113{
114 int ret;
115 int id;
116 struct platform_device *pdev;
117
Jonathan Cameron3e542812011-11-02 13:39:43 -0700118 id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
Clifton Barnes275ac742011-05-26 16:26:04 -0700119 if (id < 0) {
120 ret = id;
121 goto noid;
122 }
123
124 pdev = platform_device_alloc("ds2780-battery", id);
125 if (!pdev) {
126 ret = -ENOMEM;
127 goto pdev_alloc_failed;
128 }
129 pdev->dev.parent = &sl->dev;
130
131 ret = platform_device_add(pdev);
132 if (ret)
133 goto pdev_add_failed;
134
135 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
136 if (ret)
137 goto bin_attr_failed;
138
139 dev_set_drvdata(&sl->dev, pdev);
140
141 return 0;
142
143bin_attr_failed:
Wei Yongjunc5cfedf2013-04-30 15:28:36 -0700144 platform_device_del(pdev);
Clifton Barnes275ac742011-05-26 16:26:04 -0700145pdev_add_failed:
Wei Yongjunc5cfedf2013-04-30 15:28:36 -0700146 platform_device_put(pdev);
Clifton Barnes275ac742011-05-26 16:26:04 -0700147pdev_alloc_failed:
Jonathan Cameron3e542812011-11-02 13:39:43 -0700148 ida_simple_remove(&bat_ida, id);
Clifton Barnes275ac742011-05-26 16:26:04 -0700149noid:
150 return ret;
151}
152
153static void w1_ds2780_remove_slave(struct w1_slave *sl)
154{
155 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
156 int id = pdev->id;
157
158 platform_device_unregister(pdev);
Jonathan Cameron3e542812011-11-02 13:39:43 -0700159 ida_simple_remove(&bat_ida, id);
Clifton Barnes275ac742011-05-26 16:26:04 -0700160 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
161}
162
163static struct w1_family_ops w1_ds2780_fops = {
164 .add_slave = w1_ds2780_add_slave,
165 .remove_slave = w1_ds2780_remove_slave,
166};
167
168static struct w1_family w1_ds2780_family = {
169 .fid = W1_FAMILY_DS2780,
170 .fops = &w1_ds2780_fops,
171};
172
173static int __init w1_ds2780_init(void)
174{
Jonathan Cameron3e542812011-11-02 13:39:43 -0700175 ida_init(&bat_ida);
Clifton Barnes275ac742011-05-26 16:26:04 -0700176 return w1_register_family(&w1_ds2780_family);
177}
178
179static void __exit w1_ds2780_exit(void)
180{
181 w1_unregister_family(&w1_ds2780_family);
Jonathan Cameron3e542812011-11-02 13:39:43 -0700182 ida_destroy(&bat_ida);
Clifton Barnes275ac742011-05-26 16:26:04 -0700183}
184
185module_init(w1_ds2780_init);
186module_exit(w1_ds2780_exit);
187
188MODULE_LICENSE("GPL");
189MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
190MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");
Alexander Stein8d7bda52013-05-26 20:06:50 +0200191MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2780));