]> Git Repo - linux.git/blame - drivers/w1/slaves/w1_ds2780.c
drivers/power/ds2780_battery.c: create central point for calling w1 interface
[linux.git] / drivers / w1 / slaves / w1_ds2780.c
CommitLineData
275ac746
CB
1/*
2 * 1-Wire implementation for the ds2780 chip
3 *
4 * Copyright (C) 2010 Indesign, LLC
5 *
6 * Author: Clifton Barnes <[email protected]>
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
29int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
30 int io)
31{
32 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
33
34 if (!dev)
35 return -ENODEV;
36
37 mutex_lock(&sl->master->mutex);
38
39 if (addr > DS2780_DATA_SIZE || addr < 0) {
40 count = 0;
41 goto out;
42 }
43 count = min_t(int, count, DS2780_DATA_SIZE - addr);
44
45 if (w1_reset_select_slave(sl) == 0) {
46 if (io) {
47 w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
48 w1_write_8(sl->master, addr);
49 w1_write_block(sl->master, buf, count);
50 /* XXX w1_write_block returns void, not n_written */
51 } else {
52 w1_write_8(sl->master, W1_DS2780_READ_DATA);
53 w1_write_8(sl->master, addr);
54 count = w1_read_block(sl->master, buf, count);
55 }
56 }
57
58out:
59 mutex_unlock(&sl->master->mutex);
60
61 return count;
62}
63EXPORT_SYMBOL(w1_ds2780_io);
64
65int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
66{
67 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
68
69 if (!dev)
70 return -EINVAL;
71
72 mutex_lock(&sl->master->mutex);
73
74 if (w1_reset_select_slave(sl) == 0) {
75 w1_write_8(sl->master, cmd);
76 w1_write_8(sl->master, addr);
77 }
78
79 mutex_unlock(&sl->master->mutex);
80 return 0;
81}
82EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
83
84static ssize_t w1_ds2780_read_bin(struct file *filp,
85 struct kobject *kobj,
86 struct bin_attribute *bin_attr,
87 char *buf, loff_t off, size_t count)
88{
89 struct device *dev = container_of(kobj, struct device, kobj);
90 return w1_ds2780_io(dev, buf, off, count, 0);
91}
92
93static struct bin_attribute w1_ds2780_bin_attr = {
94 .attr = {
95 .name = "w1_slave",
96 .mode = S_IRUGO,
97 },
98 .size = DS2780_DATA_SIZE,
99 .read = w1_ds2780_read_bin,
100};
101
3e542817 102static DEFINE_IDA(bat_ida);
275ac746
CB
103
104static int w1_ds2780_add_slave(struct w1_slave *sl)
105{
106 int ret;
107 int id;
108 struct platform_device *pdev;
109
3e542817 110 id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
275ac746
CB
111 if (id < 0) {
112 ret = id;
113 goto noid;
114 }
115
116 pdev = platform_device_alloc("ds2780-battery", id);
117 if (!pdev) {
118 ret = -ENOMEM;
119 goto pdev_alloc_failed;
120 }
121 pdev->dev.parent = &sl->dev;
122
123 ret = platform_device_add(pdev);
124 if (ret)
125 goto pdev_add_failed;
126
127 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
128 if (ret)
129 goto bin_attr_failed;
130
131 dev_set_drvdata(&sl->dev, pdev);
132
133 return 0;
134
135bin_attr_failed:
136pdev_add_failed:
137 platform_device_unregister(pdev);
138pdev_alloc_failed:
3e542817 139 ida_simple_remove(&bat_ida, id);
275ac746
CB
140noid:
141 return ret;
142}
143
144static void w1_ds2780_remove_slave(struct w1_slave *sl)
145{
146 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
147 int id = pdev->id;
148
149 platform_device_unregister(pdev);
3e542817 150 ida_simple_remove(&bat_ida, id);
275ac746
CB
151 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
152}
153
154static struct w1_family_ops w1_ds2780_fops = {
155 .add_slave = w1_ds2780_add_slave,
156 .remove_slave = w1_ds2780_remove_slave,
157};
158
159static struct w1_family w1_ds2780_family = {
160 .fid = W1_FAMILY_DS2780,
161 .fops = &w1_ds2780_fops,
162};
163
164static int __init w1_ds2780_init(void)
165{
3e542817 166 ida_init(&bat_ida);
275ac746
CB
167 return w1_register_family(&w1_ds2780_family);
168}
169
170static void __exit w1_ds2780_exit(void)
171{
172 w1_unregister_family(&w1_ds2780_family);
3e542817 173 ida_destroy(&bat_ida);
275ac746
CB
174}
175
176module_init(w1_ds2780_init);
177module_exit(w1_ds2780_exit);
178
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("Clifton Barnes <[email protected]>");
181MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");
This page took 0.106105 seconds and 4 git commands to generate.