]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fef37e9a RS |
2 | /* |
3 | * 1-Wire implementation for the ds2781 chip | |
4 | * | |
5 | * Author: Renata Sayakhova <renata@oktetlabs.ru> | |
6 | * | |
7 | * Based on w1-ds2780 driver | |
fef37e9a RS |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/device.h> | |
13 | #include <linux/types.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/mutex.h> | |
fef37e9a | 16 | |
de0d6dbd AD |
17 | #include <linux/w1.h> |
18 | ||
fef37e9a RS |
19 | #include "w1_ds2781.h" |
20 | ||
de0d6dbd AD |
21 | #define W1_FAMILY_DS2781 0x3D |
22 | ||
fef37e9a RS |
23 | static int w1_ds2781_do_io(struct device *dev, char *buf, int addr, |
24 | size_t count, int io) | |
25 | { | |
26 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
27 | ||
28 | if (addr > DS2781_DATA_SIZE || addr < 0) | |
29 | return 0; | |
30 | ||
31 | count = min_t(int, count, DS2781_DATA_SIZE - addr); | |
32 | ||
33 | if (w1_reset_select_slave(sl) == 0) { | |
34 | if (io) { | |
35 | w1_write_8(sl->master, W1_DS2781_WRITE_DATA); | |
36 | w1_write_8(sl->master, addr); | |
37 | w1_write_block(sl->master, buf, count); | |
38 | } else { | |
39 | w1_write_8(sl->master, W1_DS2781_READ_DATA); | |
40 | w1_write_8(sl->master, addr); | |
41 | count = w1_read_block(sl->master, buf, count); | |
42 | } | |
43 | } | |
44 | ||
45 | return count; | |
46 | } | |
47 | ||
48 | int w1_ds2781_io(struct device *dev, char *buf, int addr, size_t count, | |
49 | int io) | |
50 | { | |
51 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
52 | int ret; | |
53 | ||
54 | if (!dev) | |
55 | return -ENODEV; | |
56 | ||
b02f8bed | 57 | mutex_lock(&sl->master->bus_mutex); |
fef37e9a RS |
58 | |
59 | ret = w1_ds2781_do_io(dev, buf, addr, count, io); | |
60 | ||
b02f8bed | 61 | mutex_unlock(&sl->master->bus_mutex); |
fef37e9a RS |
62 | |
63 | return ret; | |
64 | } | |
65 | EXPORT_SYMBOL(w1_ds2781_io); | |
66 | ||
fef37e9a RS |
67 | int w1_ds2781_eeprom_cmd(struct device *dev, int addr, int cmd) |
68 | { | |
69 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
70 | ||
71 | if (!dev) | |
72 | return -EINVAL; | |
73 | ||
b02f8bed | 74 | mutex_lock(&sl->master->bus_mutex); |
fef37e9a RS |
75 | |
76 | if (w1_reset_select_slave(sl) == 0) { | |
77 | w1_write_8(sl->master, cmd); | |
78 | w1_write_8(sl->master, addr); | |
79 | } | |
80 | ||
b02f8bed | 81 | mutex_unlock(&sl->master->bus_mutex); |
fef37e9a RS |
82 | return 0; |
83 | } | |
84 | EXPORT_SYMBOL(w1_ds2781_eeprom_cmd); | |
85 | ||
eefafb79 GKH |
86 | static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj, |
87 | struct bin_attribute *bin_attr, char *buf, | |
88 | loff_t off, size_t count) | |
fef37e9a RS |
89 | { |
90 | struct device *dev = container_of(kobj, struct device, kobj); | |
91 | return w1_ds2781_io(dev, buf, off, count, 0); | |
92 | } | |
93 | ||
eefafb79 GKH |
94 | static BIN_ATTR_RO(w1_slave, DS2781_DATA_SIZE); |
95 | ||
96 | static struct bin_attribute *w1_ds2781_bin_attrs[] = { | |
97 | &bin_attr_w1_slave, | |
98 | NULL, | |
99 | }; | |
100 | ||
101 | static const struct attribute_group w1_ds2781_group = { | |
102 | .bin_attrs = w1_ds2781_bin_attrs, | |
103 | }; | |
104 | ||
105 | static const struct attribute_group *w1_ds2781_groups[] = { | |
106 | &w1_ds2781_group, | |
107 | NULL, | |
fef37e9a RS |
108 | }; |
109 | ||
fef37e9a RS |
110 | static int w1_ds2781_add_slave(struct w1_slave *sl) |
111 | { | |
112 | int ret; | |
fef37e9a RS |
113 | struct platform_device *pdev; |
114 | ||
098f9fb0 AD |
115 | pdev = platform_device_alloc("ds2781-battery", PLATFORM_DEVID_AUTO); |
116 | if (!pdev) | |
117 | return -ENOMEM; | |
fef37e9a RS |
118 | pdev->dev.parent = &sl->dev; |
119 | ||
120 | ret = platform_device_add(pdev); | |
121 | if (ret) | |
122 | goto pdev_add_failed; | |
123 | ||
fef37e9a RS |
124 | dev_set_drvdata(&sl->dev, pdev); |
125 | ||
126 | return 0; | |
127 | ||
fef37e9a | 128 | pdev_add_failed: |
c28d6f2d | 129 | platform_device_put(pdev); |
098f9fb0 | 130 | |
fef37e9a RS |
131 | return ret; |
132 | } | |
133 | ||
134 | static void w1_ds2781_remove_slave(struct w1_slave *sl) | |
135 | { | |
136 | struct platform_device *pdev = dev_get_drvdata(&sl->dev); | |
fef37e9a RS |
137 | |
138 | platform_device_unregister(pdev); | |
fef37e9a RS |
139 | } |
140 | ||
141 | static struct w1_family_ops w1_ds2781_fops = { | |
142 | .add_slave = w1_ds2781_add_slave, | |
143 | .remove_slave = w1_ds2781_remove_slave, | |
eefafb79 | 144 | .groups = w1_ds2781_groups, |
fef37e9a RS |
145 | }; |
146 | ||
147 | static struct w1_family w1_ds2781_family = { | |
148 | .fid = W1_FAMILY_DS2781, | |
149 | .fops = &w1_ds2781_fops, | |
150 | }; | |
939fc832 | 151 | module_w1_family(w1_ds2781_family); |
fef37e9a | 152 | |
fef37e9a RS |
153 | MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>"); |
154 | MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC"); | |
50fa2951 | 155 | MODULE_LICENSE("GPL"); |
8d7bda51 | 156 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2781)); |