2 * w1_ds2413.c - w1 family 3a (DS2413) driver
3 * based on w1_ds2408.c by Jean-Francois Dagenais <dagenaisj@sonatest.com>
5 * Copyright (c) 2013 Mariusz Bialonczyk <manio@skyboo.net>
7 * This source code is licensed under the GNU General Public License,
8 * Version 2. See the file COPYING for more details.
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>
20 #include "../w1_int.h"
21 #include "../w1_family.h"
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
25 MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO");
27 #define W1_F3A_RETRIES 3
28 #define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5
29 #define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A
30 #define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA
32 static ssize_t w1_f3a_read_state(
33 struct file *filp, struct kobject *kobj,
34 struct bin_attribute *bin_attr,
35 char *buf, loff_t off, size_t count)
37 struct w1_slave *sl = kobj_to_w1_slave(kobj);
39 "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
40 bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
47 mutex_lock(&sl->master->bus_mutex);
48 dev_dbg(&sl->dev, "mutex locked");
50 if (w1_reset_select_slave(sl)) {
51 mutex_unlock(&sl->master->bus_mutex);
55 w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ);
56 *buf = w1_read_8(sl->master);
58 mutex_unlock(&sl->master->bus_mutex);
59 dev_dbg(&sl->dev, "mutex unlocked");
61 /* check for correct complement */
62 if ((*buf & 0x0F) != ((~*buf >> 4) & 0x0F))
68 static ssize_t w1_f3a_write_output(
69 struct file *filp, struct kobject *kobj,
70 struct bin_attribute *bin_attr,
71 char *buf, loff_t off, size_t count)
73 struct w1_slave *sl = kobj_to_w1_slave(kobj);
75 unsigned int retries = W1_F3A_RETRIES;
77 if (count != 1 || off != 0)
80 dev_dbg(&sl->dev, "locking mutex for write_output");
81 mutex_lock(&sl->master->bus_mutex);
82 dev_dbg(&sl->dev, "mutex locked");
84 if (w1_reset_select_slave(sl))
87 /* according to the DS2413 datasheet the most significant 6 bits
88 should be set to "1"s, so do it now */
92 w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE;
95 w1_write_block(sl->master, w1_buf, 3);
97 if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) {
98 mutex_unlock(&sl->master->bus_mutex);
99 dev_dbg(&sl->dev, "mutex unlocked, retries:%d", retries);
102 if (w1_reset_resume_command(sl->master))
107 mutex_unlock(&sl->master->bus_mutex);
108 dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);
112 #define NB_SYSFS_BIN_FILES 2
113 static struct bin_attribute w1_f3a_sysfs_bin_files[NB_SYSFS_BIN_FILES] = {
120 .read = w1_f3a_read_state,
125 .mode = S_IRUGO | S_IWUSR | S_IWGRP,
128 .write = w1_f3a_write_output,
132 static int w1_f3a_add_slave(struct w1_slave *sl)
137 for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
138 err = sysfs_create_bin_file(
140 &(w1_f3a_sysfs_bin_files[i]));
143 sysfs_remove_bin_file(&sl->dev.kobj,
144 &(w1_f3a_sysfs_bin_files[i]));
148 static void w1_f3a_remove_slave(struct w1_slave *sl)
151 for (i = NB_SYSFS_BIN_FILES - 1; i >= 0; --i)
152 sysfs_remove_bin_file(&sl->dev.kobj,
153 &(w1_f3a_sysfs_bin_files[i]));
156 static struct w1_family_ops w1_f3a_fops = {
157 .add_slave = w1_f3a_add_slave,
158 .remove_slave = w1_f3a_remove_slave,
161 static struct w1_family w1_family_3a = {
162 .fid = W1_FAMILY_DS2413,
163 .fops = &w1_f3a_fops,
166 static int __init w1_f3a_init(void)
168 return w1_register_family(&w1_family_3a);
171 static void __exit w1_f3a_exit(void)
173 w1_unregister_family(&w1_family_3a);
176 module_init(w1_f3a_init);
177 module_exit(w1_f3a_exit);