blob: a60528131154b5c4b54a3704e1d6b0f9ce5e3397 [file] [log] [blame]
Clifton Barnes275ac742011-05-26 16:26:04 -07001/*
2 * 1-Wire implementation for the ds2780 chip
3 *
4 * Copyright (C) 2010 Indesign, LLC
5 *
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
7 *
8 * Based on w1-ds2760 driver
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/types.h>
20#include <linux/platform_device.h>
21#include <linux/mutex.h>
22#include <linux/idr.h>
23
Andrew F. Davisde0d6db2017-06-05 08:52:08 -050024#include <linux/w1.h>
25
Clifton Barnes275ac742011-05-26 16:26:04 -070026#include "w1_ds2780.h"
27
Andrew F. Davisde0d6db2017-06-05 08:52:08 -050028#define W1_FAMILY_DS2780 0x32
29
Clifton Barnes9fe678f2011-11-02 13:39:52 -070030static int w1_ds2780_do_io(struct device *dev, char *buf, int addr,
31 size_t count, int io)
Clifton Barnes275ac742011-05-26 16:26:04 -070032{
33 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
34
Clifton Barnes9fe678f2011-11-02 13:39:52 -070035 if (addr > DS2780_DATA_SIZE || addr < 0)
36 return 0;
Clifton Barnes275ac742011-05-26 16:26:04 -070037
Clifton Barnes275ac742011-05-26 16:26:04 -070038 count = min_t(int, count, DS2780_DATA_SIZE - addr);
39
40 if (w1_reset_select_slave(sl) == 0) {
41 if (io) {
42 w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
43 w1_write_8(sl->master, addr);
44 w1_write_block(sl->master, buf, count);
Clifton Barnes275ac742011-05-26 16:26:04 -070045 } else {
46 w1_write_8(sl->master, W1_DS2780_READ_DATA);
47 w1_write_8(sl->master, addr);
48 count = w1_read_block(sl->master, buf, count);
49 }
50 }
51
Clifton Barnes275ac742011-05-26 16:26:04 -070052 return count;
53}
Clifton Barnes9fe678f2011-11-02 13:39:52 -070054
55int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
56 int io)
57{
58 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
59 int ret;
60
61 if (!dev)
62 return -ENODEV;
63
NeilBrownb02f8be2012-05-18 15:59:52 +100064 mutex_lock(&sl->master->bus_mutex);
Clifton Barnes9fe678f2011-11-02 13:39:52 -070065
66 ret = w1_ds2780_do_io(dev, buf, addr, count, io);
67
NeilBrownb02f8be2012-05-18 15:59:52 +100068 mutex_unlock(&sl->master->bus_mutex);
Clifton Barnes9fe678f2011-11-02 13:39:52 -070069
70 return ret;
71}
Clifton Barnes275ac742011-05-26 16:26:04 -070072EXPORT_SYMBOL(w1_ds2780_io);
73
74int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
75{
76 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
77
78 if (!dev)
79 return -EINVAL;
80
NeilBrownb02f8be2012-05-18 15:59:52 +100081 mutex_lock(&sl->master->bus_mutex);
Clifton Barnes275ac742011-05-26 16:26:04 -070082
83 if (w1_reset_select_slave(sl) == 0) {
84 w1_write_8(sl->master, cmd);
85 w1_write_8(sl->master, addr);
86 }
87
NeilBrownb02f8be2012-05-18 15:59:52 +100088 mutex_unlock(&sl->master->bus_mutex);
Clifton Barnes275ac742011-05-26 16:26:04 -070089 return 0;
90}
91EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
92
Greg Kroah-Hartman93652612013-08-21 15:45:03 -070093static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
94 struct bin_attribute *bin_attr, char *buf,
95 loff_t off, size_t count)
Clifton Barnes275ac742011-05-26 16:26:04 -070096{
97 struct device *dev = container_of(kobj, struct device, kobj);
98 return w1_ds2780_io(dev, buf, off, count, 0);
99}
100
Greg Kroah-Hartman93652612013-08-21 15:45:03 -0700101static BIN_ATTR_RO(w1_slave, DS2780_DATA_SIZE);
102
103static struct bin_attribute *w1_ds2780_bin_attrs[] = {
104 &bin_attr_w1_slave,
105 NULL,
106};
107
108static const struct attribute_group w1_ds2780_group = {
109 .bin_attrs = w1_ds2780_bin_attrs,
110};
111
112static const struct attribute_group *w1_ds2780_groups[] = {
113 &w1_ds2780_group,
114 NULL,
Clifton Barnes275ac742011-05-26 16:26:04 -0700115};
116
Clifton Barnes275ac742011-05-26 16:26:04 -0700117static int w1_ds2780_add_slave(struct w1_slave *sl)
118{
119 int ret;
Clifton Barnes275ac742011-05-26 16:26:04 -0700120 struct platform_device *pdev;
121
Andrew F. Davis098f9fb2016-08-02 14:07:06 -0700122 pdev = platform_device_alloc("ds2780-battery", PLATFORM_DEVID_AUTO);
123 if (!pdev)
124 return -ENOMEM;
Clifton Barnes275ac742011-05-26 16:26:04 -0700125 pdev->dev.parent = &sl->dev;
126
127 ret = platform_device_add(pdev);
128 if (ret)
129 goto pdev_add_failed;
130
Clifton Barnes275ac742011-05-26 16:26:04 -0700131 dev_set_drvdata(&sl->dev, pdev);
132
133 return 0;
134
Clifton Barnes275ac742011-05-26 16:26:04 -0700135pdev_add_failed:
Wei Yongjunc5cfedf2013-04-30 15:28:36 -0700136 platform_device_put(pdev);
Andrew F. Davis098f9fb2016-08-02 14:07:06 -0700137
Clifton Barnes275ac742011-05-26 16:26:04 -0700138 return ret;
139}
140
141static void w1_ds2780_remove_slave(struct w1_slave *sl)
142{
143 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
Clifton Barnes275ac742011-05-26 16:26:04 -0700144
145 platform_device_unregister(pdev);
Clifton Barnes275ac742011-05-26 16:26:04 -0700146}
147
148static struct w1_family_ops w1_ds2780_fops = {
149 .add_slave = w1_ds2780_add_slave,
150 .remove_slave = w1_ds2780_remove_slave,
Greg Kroah-Hartman93652612013-08-21 15:45:03 -0700151 .groups = w1_ds2780_groups,
Clifton Barnes275ac742011-05-26 16:26:04 -0700152};
153
154static struct w1_family w1_ds2780_family = {
155 .fid = W1_FAMILY_DS2780,
156 .fops = &w1_ds2780_fops,
157};
Andrew F. Davis939fc832016-08-02 14:07:09 -0700158module_w1_family(w1_ds2780_family);
Clifton Barnes275ac742011-05-26 16:26:04 -0700159
Clifton Barnes275ac742011-05-26 16:26:04 -0700160MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
161MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");
Andrew F. Davis50fa2952017-05-16 15:02:12 -0500162MODULE_LICENSE("GPL");
Alexander Stein8d7bda52013-05-26 20:06:50 +0200163MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2780));