Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 1 | /* |
| 2 | * 1-Wire implementation for the ds2760 chip |
| 3 | * |
| 4 | * Copyright © 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu> |
| 5 | * |
| 6 | * Use consistent with the GNU GPL is permitted, |
| 7 | * provided that this copyright notice is |
| 8 | * preserved in its entirety in all copies and derived works. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/gfp.h> |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 20 | |
| 21 | #include "../w1.h" |
| 22 | #include "../w1_int.h" |
| 23 | #include "../w1_family.h" |
| 24 | #include "w1_ds2760.h" |
| 25 | |
| 26 | static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count, |
| 27 | int io) |
| 28 | { |
| 29 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); |
| 30 | |
| 31 | if (!dev) |
| 32 | return 0; |
| 33 | |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 34 | mutex_lock(&sl->master->bus_mutex); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 35 | |
| 36 | if (addr > DS2760_DATA_SIZE || addr < 0) { |
| 37 | count = 0; |
| 38 | goto out; |
| 39 | } |
| 40 | if (addr + count > DS2760_DATA_SIZE) |
| 41 | count = DS2760_DATA_SIZE - addr; |
| 42 | |
| 43 | if (!w1_reset_select_slave(sl)) { |
| 44 | if (!io) { |
| 45 | w1_write_8(sl->master, W1_DS2760_READ_DATA); |
| 46 | w1_write_8(sl->master, addr); |
| 47 | count = w1_read_block(sl->master, buf, count); |
| 48 | } else { |
| 49 | w1_write_8(sl->master, W1_DS2760_WRITE_DATA); |
| 50 | w1_write_8(sl->master, addr); |
| 51 | w1_write_block(sl->master, buf, count); |
| 52 | /* XXX w1_write_block returns void, not n_written */ |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | out: |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 57 | mutex_unlock(&sl->master->bus_mutex); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 58 | |
| 59 | return count; |
| 60 | } |
| 61 | |
| 62 | int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count) |
| 63 | { |
| 64 | return w1_ds2760_io(dev, buf, addr, count, 0); |
| 65 | } |
| 66 | |
| 67 | int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count) |
| 68 | { |
| 69 | return w1_ds2760_io(dev, buf, addr, count, 1); |
| 70 | } |
| 71 | |
Daniel Mack | 0b47b57 | 2009-04-28 10:55:01 +0200 | [diff] [blame] | 72 | static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd) |
| 73 | { |
| 74 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); |
| 75 | |
| 76 | if (!dev) |
| 77 | return -EINVAL; |
| 78 | |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 79 | mutex_lock(&sl->master->bus_mutex); |
Daniel Mack | 0b47b57 | 2009-04-28 10:55:01 +0200 | [diff] [blame] | 80 | |
| 81 | if (w1_reset_select_slave(sl) == 0) { |
| 82 | w1_write_8(sl->master, cmd); |
| 83 | w1_write_8(sl->master, addr); |
| 84 | } |
| 85 | |
NeilBrown | b02f8be | 2012-05-18 15:59:52 +1000 | [diff] [blame] | 86 | mutex_unlock(&sl->master->bus_mutex); |
Daniel Mack | 0b47b57 | 2009-04-28 10:55:01 +0200 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int w1_ds2760_store_eeprom(struct device *dev, int addr) |
| 91 | { |
| 92 | return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA); |
| 93 | } |
| 94 | |
| 95 | int w1_ds2760_recall_eeprom(struct device *dev, int addr) |
| 96 | { |
| 97 | return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA); |
| 98 | } |
| 99 | |
Greg Kroah-Hartman | 0597129 | 2013-08-21 15:45:04 -0700 | [diff] [blame] | 100 | static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj, |
| 101 | struct bin_attribute *bin_attr, char *buf, |
| 102 | loff_t off, size_t count) |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 103 | { |
| 104 | struct device *dev = container_of(kobj, struct device, kobj); |
| 105 | return w1_ds2760_read(dev, buf, off, count); |
| 106 | } |
| 107 | |
Greg Kroah-Hartman | 0597129 | 2013-08-21 15:45:04 -0700 | [diff] [blame] | 108 | static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE); |
| 109 | |
| 110 | static struct bin_attribute *w1_ds2760_bin_attrs[] = { |
| 111 | &bin_attr_w1_slave, |
| 112 | NULL, |
| 113 | }; |
| 114 | |
| 115 | static const struct attribute_group w1_ds2760_group = { |
| 116 | .bin_attrs = w1_ds2760_bin_attrs, |
| 117 | }; |
| 118 | |
| 119 | static const struct attribute_group *w1_ds2760_groups[] = { |
| 120 | &w1_ds2760_group, |
| 121 | NULL, |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 122 | }; |
| 123 | |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 124 | static DEFINE_IDA(bat_ida); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 125 | |
| 126 | static int w1_ds2760_add_slave(struct w1_slave *sl) |
| 127 | { |
| 128 | int ret; |
| 129 | int id; |
| 130 | struct platform_device *pdev; |
| 131 | |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 132 | id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 133 | if (id < 0) { |
| 134 | ret = id; |
| 135 | goto noid; |
| 136 | } |
| 137 | |
| 138 | pdev = platform_device_alloc("ds2760-battery", id); |
| 139 | if (!pdev) { |
| 140 | ret = -ENOMEM; |
| 141 | goto pdev_alloc_failed; |
| 142 | } |
| 143 | pdev->dev.parent = &sl->dev; |
| 144 | |
| 145 | ret = platform_device_add(pdev); |
| 146 | if (ret) |
| 147 | goto pdev_add_failed; |
| 148 | |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 149 | dev_set_drvdata(&sl->dev, pdev); |
| 150 | |
| 151 | goto success; |
| 152 | |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 153 | pdev_add_failed: |
Wei Yongjun | 4d10e0f | 2013-04-30 15:28:38 -0700 | [diff] [blame] | 154 | platform_device_put(pdev); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 155 | pdev_alloc_failed: |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 156 | ida_simple_remove(&bat_ida, id); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 157 | noid: |
| 158 | success: |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static void w1_ds2760_remove_slave(struct w1_slave *sl) |
| 163 | { |
| 164 | struct platform_device *pdev = dev_get_drvdata(&sl->dev); |
| 165 | int id = pdev->id; |
| 166 | |
| 167 | platform_device_unregister(pdev); |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 168 | ida_simple_remove(&bat_ida, id); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static struct w1_family_ops w1_ds2760_fops = { |
| 172 | .add_slave = w1_ds2760_add_slave, |
| 173 | .remove_slave = w1_ds2760_remove_slave, |
Greg Kroah-Hartman | 0597129 | 2013-08-21 15:45:04 -0700 | [diff] [blame] | 174 | .groups = w1_ds2760_groups, |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static struct w1_family w1_ds2760_family = { |
| 178 | .fid = W1_FAMILY_DS2760, |
| 179 | .fops = &w1_ds2760_fops, |
| 180 | }; |
| 181 | |
| 182 | static int __init w1_ds2760_init(void) |
| 183 | { |
| 184 | printk(KERN_INFO "1-Wire driver for the DS2760 battery monitor " |
| 185 | " chip - (c) 2004-2005, Szabolcs Gyurko\n"); |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 186 | ida_init(&bat_ida); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 187 | return w1_register_family(&w1_ds2760_family); |
| 188 | } |
| 189 | |
| 190 | static void __exit w1_ds2760_exit(void) |
| 191 | { |
| 192 | w1_unregister_family(&w1_ds2760_family); |
Jonathan Cameron | 3e54281 | 2011-11-02 13:39:43 -0700 | [diff] [blame] | 193 | ida_destroy(&bat_ida); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | EXPORT_SYMBOL(w1_ds2760_read); |
| 197 | EXPORT_SYMBOL(w1_ds2760_write); |
Daniel Mack | 0b47b57 | 2009-04-28 10:55:01 +0200 | [diff] [blame] | 198 | EXPORT_SYMBOL(w1_ds2760_store_eeprom); |
| 199 | EXPORT_SYMBOL(w1_ds2760_recall_eeprom); |
Anton Vorontsov | d7ce6d1 | 2007-04-12 01:03:55 +0400 | [diff] [blame] | 200 | |
| 201 | module_init(w1_ds2760_init); |
| 202 | module_exit(w1_ds2760_exit); |
| 203 | |
| 204 | MODULE_LICENSE("GPL"); |
| 205 | MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>"); |
| 206 | MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip"); |
Alexander Stein | 8d7bda5 | 2013-05-26 20:06:50 +0200 | [diff] [blame] | 207 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760)); |