Commit | Line | Data |
---|---|---|
31b4ca3e MB |
1 | /* |
2 | * w1_ds2413.c - w1 family 3a (DS2413) driver | |
3 | * based on w1_ds2408.c by Jean-Francois Dagenais <dagenaisj@sonatest.com> | |
4 | * | |
5 | * Copyright (c) 2013 Mariusz Bialonczyk <manio@skyboo.net> | |
6 | * | |
7 | * This source code is licensed under the GNU General Public License, | |
8 | * Version 2. See the file COPYING for more details. | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/moduleparam.h> | |
14 | #include <linux/device.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/slab.h> | |
18 | ||
de0d6dbd AD |
19 | #include <linux/w1.h> |
20 | ||
21 | #define W1_FAMILY_DS2413 0x3A | |
31b4ca3e | 22 | |
31b4ca3e MB |
23 | #define W1_F3A_RETRIES 3 |
24 | #define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5 | |
25 | #define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A | |
26 | #define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA | |
27 | ||
729fb9d3 GKH |
28 | static ssize_t state_read(struct file *filp, struct kobject *kobj, |
29 | struct bin_attribute *bin_attr, char *buf, loff_t off, | |
30 | size_t count) | |
31b4ca3e MB |
31 | { |
32 | struct w1_slave *sl = kobj_to_w1_slave(kobj); | |
33 | dev_dbg(&sl->dev, | |
34 | "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p", | |
35 | bin_attr->attr.name, kobj, (unsigned int)off, count, buf); | |
36 | ||
37 | if (off != 0) | |
38 | return 0; | |
39 | if (!buf) | |
40 | return -EINVAL; | |
41 | ||
42 | mutex_lock(&sl->master->bus_mutex); | |
43 | dev_dbg(&sl->dev, "mutex locked"); | |
44 | ||
45 | if (w1_reset_select_slave(sl)) { | |
46 | mutex_unlock(&sl->master->bus_mutex); | |
47 | return -EIO; | |
48 | } | |
49 | ||
50 | w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ); | |
51 | *buf = w1_read_8(sl->master); | |
52 | ||
53 | mutex_unlock(&sl->master->bus_mutex); | |
54 | dev_dbg(&sl->dev, "mutex unlocked"); | |
55 | ||
56 | /* check for correct complement */ | |
57 | if ((*buf & 0x0F) != ((~*buf >> 4) & 0x0F)) | |
58 | return -EIO; | |
59 | else | |
60 | return 1; | |
61 | } | |
62 | ||
729fb9d3 GKH |
63 | static BIN_ATTR_RO(state, 1); |
64 | ||
65 | static ssize_t output_write(struct file *filp, struct kobject *kobj, | |
66 | struct bin_attribute *bin_attr, char *buf, | |
67 | loff_t off, size_t count) | |
31b4ca3e MB |
68 | { |
69 | struct w1_slave *sl = kobj_to_w1_slave(kobj); | |
70 | u8 w1_buf[3]; | |
71 | unsigned int retries = W1_F3A_RETRIES; | |
72 | ||
73 | if (count != 1 || off != 0) | |
74 | return -EFAULT; | |
75 | ||
76 | dev_dbg(&sl->dev, "locking mutex for write_output"); | |
77 | mutex_lock(&sl->master->bus_mutex); | |
78 | dev_dbg(&sl->dev, "mutex locked"); | |
79 | ||
80 | if (w1_reset_select_slave(sl)) | |
81 | goto error; | |
82 | ||
83 | /* according to the DS2413 datasheet the most significant 6 bits | |
84 | should be set to "1"s, so do it now */ | |
85 | *buf = *buf | 0xFC; | |
86 | ||
87 | while (retries--) { | |
88 | w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE; | |
89 | w1_buf[1] = *buf; | |
90 | w1_buf[2] = ~(*buf); | |
91 | w1_write_block(sl->master, w1_buf, 3); | |
92 | ||
93 | if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) { | |
94 | mutex_unlock(&sl->master->bus_mutex); | |
95 | dev_dbg(&sl->dev, "mutex unlocked, retries:%d", retries); | |
96 | return 1; | |
97 | } | |
98 | if (w1_reset_resume_command(sl->master)) | |
99 | goto error; | |
100 | } | |
101 | ||
102 | error: | |
103 | mutex_unlock(&sl->master->bus_mutex); | |
104 | dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries); | |
105 | return -EIO; | |
106 | } | |
107 | ||
729fb9d3 GKH |
108 | static BIN_ATTR(output, S_IRUGO | S_IWUSR | S_IWGRP, NULL, output_write, 1); |
109 | ||
110 | static struct bin_attribute *w1_f3a_bin_attrs[] = { | |
111 | &bin_attr_state, | |
112 | &bin_attr_output, | |
113 | NULL, | |
31b4ca3e MB |
114 | }; |
115 | ||
729fb9d3 GKH |
116 | static const struct attribute_group w1_f3a_group = { |
117 | .bin_attrs = w1_f3a_bin_attrs, | |
118 | }; | |
31b4ca3e | 119 | |
729fb9d3 GKH |
120 | static const struct attribute_group *w1_f3a_groups[] = { |
121 | &w1_f3a_group, | |
122 | NULL, | |
123 | }; | |
31b4ca3e MB |
124 | |
125 | static struct w1_family_ops w1_f3a_fops = { | |
729fb9d3 | 126 | .groups = w1_f3a_groups, |
31b4ca3e MB |
127 | }; |
128 | ||
129 | static struct w1_family w1_family_3a = { | |
130 | .fid = W1_FAMILY_DS2413, | |
131 | .fops = &w1_f3a_fops, | |
132 | }; | |
939fc832 | 133 | module_w1_family(w1_family_3a); |
50fa2951 AD |
134 | |
135 | MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>"); | |
136 | MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO"); | |
137 | MODULE_LICENSE("GPL"); | |
138 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413)); |