blob: f2e1c51533b957f480ee8eb78f1d01a39df81c16 [file] [log] [blame]
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -08001/*
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
19#include "../w1.h"
20#include "../w1_int.h"
21#include "../w1_family.h"
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Mariusz Bialonczyk <manio@skyboo.net>");
25MODULE_DESCRIPTION("w1 family 3a driver for DS2413 2 Pin IO");
Alexander Stein8d7bda52013-05-26 20:06:50 +020026MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2413));
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -080027
28#define W1_F3A_RETRIES 3
29#define W1_F3A_FUNC_PIO_ACCESS_READ 0xF5
30#define W1_F3A_FUNC_PIO_ACCESS_WRITE 0x5A
31#define W1_F3A_SUCCESS_CONFIRM_BYTE 0xAA
32
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -070033static ssize_t state_read(struct file *filp, struct kobject *kobj,
34 struct bin_attribute *bin_attr, char *buf, loff_t off,
35 size_t count)
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -080036{
37 struct w1_slave *sl = kobj_to_w1_slave(kobj);
38 dev_dbg(&sl->dev,
39 "Reading %s kobj: %p, off: %0#10x, count: %zu, buff addr: %p",
40 bin_attr->attr.name, kobj, (unsigned int)off, count, buf);
41
42 if (off != 0)
43 return 0;
44 if (!buf)
45 return -EINVAL;
46
47 mutex_lock(&sl->master->bus_mutex);
48 dev_dbg(&sl->dev, "mutex locked");
49
50 if (w1_reset_select_slave(sl)) {
51 mutex_unlock(&sl->master->bus_mutex);
52 return -EIO;
53 }
54
55 w1_write_8(sl->master, W1_F3A_FUNC_PIO_ACCESS_READ);
56 *buf = w1_read_8(sl->master);
57
58 mutex_unlock(&sl->master->bus_mutex);
59 dev_dbg(&sl->dev, "mutex unlocked");
60
61 /* check for correct complement */
62 if ((*buf & 0x0F) != ((~*buf >> 4) & 0x0F))
63 return -EIO;
64 else
65 return 1;
66}
67
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -070068static BIN_ATTR_RO(state, 1);
69
70static ssize_t output_write(struct file *filp, struct kobject *kobj,
71 struct bin_attribute *bin_attr, char *buf,
72 loff_t off, size_t count)
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -080073{
74 struct w1_slave *sl = kobj_to_w1_slave(kobj);
75 u8 w1_buf[3];
76 unsigned int retries = W1_F3A_RETRIES;
77
78 if (count != 1 || off != 0)
79 return -EFAULT;
80
81 dev_dbg(&sl->dev, "locking mutex for write_output");
82 mutex_lock(&sl->master->bus_mutex);
83 dev_dbg(&sl->dev, "mutex locked");
84
85 if (w1_reset_select_slave(sl))
86 goto error;
87
88 /* according to the DS2413 datasheet the most significant 6 bits
89 should be set to "1"s, so do it now */
90 *buf = *buf | 0xFC;
91
92 while (retries--) {
93 w1_buf[0] = W1_F3A_FUNC_PIO_ACCESS_WRITE;
94 w1_buf[1] = *buf;
95 w1_buf[2] = ~(*buf);
96 w1_write_block(sl->master, w1_buf, 3);
97
98 if (w1_read_8(sl->master) == W1_F3A_SUCCESS_CONFIRM_BYTE) {
99 mutex_unlock(&sl->master->bus_mutex);
100 dev_dbg(&sl->dev, "mutex unlocked, retries:%d", retries);
101 return 1;
102 }
103 if (w1_reset_resume_command(sl->master))
104 goto error;
105 }
106
107error:
108 mutex_unlock(&sl->master->bus_mutex);
109 dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);
110 return -EIO;
111}
112
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -0700113static BIN_ATTR(output, S_IRUGO | S_IWUSR | S_IWGRP, NULL, output_write, 1);
114
115static struct bin_attribute *w1_f3a_bin_attrs[] = {
116 &bin_attr_state,
117 &bin_attr_output,
118 NULL,
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -0800119};
120
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -0700121static const struct attribute_group w1_f3a_group = {
122 .bin_attrs = w1_f3a_bin_attrs,
123};
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -0800124
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -0700125static const struct attribute_group *w1_f3a_groups[] = {
126 &w1_f3a_group,
127 NULL,
128};
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -0800129
130static struct w1_family_ops w1_f3a_fops = {
Greg Kroah-Hartman729fb9d2013-08-21 15:44:58 -0700131 .groups = w1_f3a_groups,
Mariusz Bialonczyk31b4ca32013-02-27 17:05:47 -0800132};
133
134static struct w1_family w1_family_3a = {
135 .fid = W1_FAMILY_DS2413,
136 .fops = &w1_f3a_fops,
137};
Andrew F. Davis939fc832016-08-02 14:07:09 -0700138module_w1_family(w1_family_3a);