Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
w1_ds2760.c
Go to the documentation of this file.
1 /*
2  * 1-Wire implementation for the ds2760 chip
3  *
4  * Copyright © 2004-2005, Szabolcs Gyurko <[email protected]>
5  *
6  * Use consistent with the GNU GPL is permitted,
7  * provided that this copyright notice is
8  * preserved in its entirety in all copies and derived works.
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>
18 #include <linux/idr.h>
19 #include <linux/gfp.h>
20 
21 #include "../w1.h"
22 #include "../w1_int.h"
23 #include "../w1_family.h"
24 #include "w1_ds2760.h"
25 
26 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
27  int io)
28 {
29  struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
30 
31  if (!dev)
32  return 0;
33 
34  mutex_lock(&sl->master->bus_mutex);
35 
36  if (addr > DS2760_DATA_SIZE || addr < 0) {
37  count = 0;
38  goto out;
39  }
40  if (addr + count > DS2760_DATA_SIZE)
41  count = DS2760_DATA_SIZE - addr;
42 
43  if (!w1_reset_select_slave(sl)) {
44  if (!io) {
45  w1_write_8(sl->master, W1_DS2760_READ_DATA);
46  w1_write_8(sl->master, addr);
47  count = w1_read_block(sl->master, buf, count);
48  } else {
49  w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
50  w1_write_8(sl->master, addr);
51  w1_write_block(sl->master, buf, count);
52  /* XXX w1_write_block returns void, not n_written */
53  }
54  }
55 
56 out:
57  mutex_unlock(&sl->master->bus_mutex);
58 
59  return count;
60 }
61 
62 int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
63 {
64  return w1_ds2760_io(dev, buf, addr, count, 0);
65 }
66 
67 int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
68 {
69  return w1_ds2760_io(dev, buf, addr, count, 1);
70 }
71 
72 static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
73 {
74  struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
75 
76  if (!dev)
77  return -EINVAL;
78 
79  mutex_lock(&sl->master->bus_mutex);
80 
81  if (w1_reset_select_slave(sl) == 0) {
82  w1_write_8(sl->master, cmd);
83  w1_write_8(sl->master, addr);
84  }
85 
86  mutex_unlock(&sl->master->bus_mutex);
87  return 0;
88 }
89 
90 int w1_ds2760_store_eeprom(struct device *dev, int addr)
91 {
92  return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
93 }
94 
95 int w1_ds2760_recall_eeprom(struct device *dev, int addr)
96 {
97  return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
98 }
99 
100 static ssize_t w1_ds2760_read_bin(struct file *filp, struct kobject *kobj,
101  struct bin_attribute *bin_attr,
102  char *buf, loff_t off, size_t count)
103 {
104  struct device *dev = container_of(kobj, struct device, kobj);
105  return w1_ds2760_read(dev, buf, off, count);
106 }
107 
108 static struct bin_attribute w1_ds2760_bin_attr = {
109  .attr = {
110  .name = "w1_slave",
111  .mode = S_IRUGO,
112  },
113  .size = DS2760_DATA_SIZE,
114  .read = w1_ds2760_read_bin,
115 };
116 
117 static DEFINE_IDA(bat_ida);
118 
119 static int w1_ds2760_add_slave(struct w1_slave *sl)
120 {
121  int ret;
122  int id;
123  struct platform_device *pdev;
124 
125  id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL);
126  if (id < 0) {
127  ret = id;
128  goto noid;
129  }
130 
131  pdev = platform_device_alloc("ds2760-battery", id);
132  if (!pdev) {
133  ret = -ENOMEM;
134  goto pdev_alloc_failed;
135  }
136  pdev->dev.parent = &sl->dev;
137 
138  ret = platform_device_add(pdev);
139  if (ret)
140  goto pdev_add_failed;
141 
142  ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
143  if (ret)
144  goto bin_attr_failed;
145 
146  dev_set_drvdata(&sl->dev, pdev);
147 
148  goto success;
149 
150 bin_attr_failed:
151 pdev_add_failed:
153 pdev_alloc_failed:
154  ida_simple_remove(&bat_ida, id);
155 noid:
156 success:
157  return ret;
158 }
159 
160 static void w1_ds2760_remove_slave(struct w1_slave *sl)
161 {
162  struct platform_device *pdev = dev_get_drvdata(&sl->dev);
163  int id = pdev->id;
164 
166  ida_simple_remove(&bat_ida, id);
167  sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
168 }
169 
170 static struct w1_family_ops w1_ds2760_fops = {
171  .add_slave = w1_ds2760_add_slave,
172  .remove_slave = w1_ds2760_remove_slave,
173 };
174 
175 static struct w1_family w1_ds2760_family = {
176  .fid = W1_FAMILY_DS2760,
177  .fops = &w1_ds2760_fops,
178 };
179 
180 static int __init w1_ds2760_init(void)
181 {
182  printk(KERN_INFO "1-Wire driver for the DS2760 battery monitor "
183  " chip - (c) 2004-2005, Szabolcs Gyurko\n");
184  ida_init(&bat_ida);
185  return w1_register_family(&w1_ds2760_family);
186 }
187 
188 static void __exit w1_ds2760_exit(void)
189 {
190  w1_unregister_family(&w1_ds2760_family);
191  ida_destroy(&bat_ida);
192 }
193 
198 
199 module_init(w1_ds2760_init);
200 module_exit(w1_ds2760_exit);
201 
202 MODULE_LICENSE("GPL");
203 MODULE_AUTHOR("Szabolcs Gyurko <[email protected]>");
204 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");