/drivers/w1/slaves/w1_ds2780.c
C | 239 lines | 177 code | 48 blank | 14 comment | 24 complexity | 19e0171f03d7fbcdcdc14bf3dbbf0a69 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
1/* 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 24#include "../w1.h" 25#include "../w1_int.h" 26#include "../w1_family.h" 27#include "w1_ds2780.h" 28 29static int w1_ds2780_do_io(struct device *dev, char *buf, int addr, 30 size_t count, int io) 31{ 32 struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 33 34 if (addr > DS2780_DATA_SIZE || addr < 0) 35 return 0; 36 37 count = min_t(int, count, DS2780_DATA_SIZE - addr); 38 39 if (w1_reset_select_slave(sl) == 0) { 40 if (io) { 41 w1_write_8(sl->master, W1_DS2780_WRITE_DATA); 42 w1_write_8(sl->master, addr); 43 w1_write_block(sl->master, buf, count); 44 } else { 45 w1_write_8(sl->master, W1_DS2780_READ_DATA); 46 w1_write_8(sl->master, addr); 47 count = w1_read_block(sl->master, buf, count); 48 } 49 } 50 51 return count; 52} 53 54int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count, 55 int io) 56{ 57 struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 58 int ret; 59 60 if (!dev) 61 return -ENODEV; 62 63 mutex_lock(&sl->master->mutex); 64 65 ret = w1_ds2780_do_io(dev, buf, addr, count, io); 66 67 mutex_unlock(&sl->master->mutex); 68 69 return ret; 70} 71EXPORT_SYMBOL(w1_ds2780_io); 72 73int w1_ds2780_io_nolock(struct device *dev, char *buf, int addr, size_t count, 74 int io) 75{ 76 int ret; 77 78 if (!dev) 79 return -ENODEV; 80 81 ret = w1_ds2780_do_io(dev, buf, addr, count, io); 82 83 return ret; 84} 85EXPORT_SYMBOL(w1_ds2780_io_nolock); 86 87int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd) 88{ 89 struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 90 91 if (!dev) 92 return -EINVAL; 93 94 mutex_lock(&sl->master->mutex); 95 96 if (w1_reset_select_slave(sl) == 0) { 97 w1_write_8(sl->master, cmd); 98 w1_write_8(sl->master, addr); 99 } 100 101 mutex_unlock(&sl->master->mutex); 102 return 0; 103} 104EXPORT_SYMBOL(w1_ds2780_eeprom_cmd); 105 106static ssize_t w1_ds2780_read_bin(struct file *filp, 107 struct kobject *kobj, 108 struct bin_attribute *bin_attr, 109 char *buf, loff_t off, size_t count) 110{ 111 struct device *dev = container_of(kobj, struct device, kobj); 112 return w1_ds2780_io(dev, buf, off, count, 0); 113} 114 115static struct bin_attribute w1_ds2780_bin_attr = { 116 .attr = { 117 .name = "w1_slave", 118 .mode = S_IRUGO, 119 }, 120 .size = DS2780_DATA_SIZE, 121 .read = w1_ds2780_read_bin, 122}; 123 124static DEFINE_IDR(bat_idr); 125static DEFINE_MUTEX(bat_idr_lock); 126 127static int new_bat_id(void) 128{ 129 int ret; 130 131 while (1) { 132 int id; 133 134 ret = idr_pre_get(&bat_idr, GFP_KERNEL); 135 if (ret == 0) 136 return -ENOMEM; 137 138 mutex_lock(&bat_idr_lock); 139 ret = idr_get_new(&bat_idr, NULL, &id); 140 mutex_unlock(&bat_idr_lock); 141 142 if (ret == 0) { 143 ret = id & MAX_ID_MASK; 144 break; 145 } else if (ret == -EAGAIN) { 146 continue; 147 } else { 148 break; 149 } 150 } 151 152 return ret; 153} 154 155static void release_bat_id(int id) 156{ 157 mutex_lock(&bat_idr_lock); 158 idr_remove(&bat_idr, id); 159 mutex_unlock(&bat_idr_lock); 160} 161 162static int w1_ds2780_add_slave(struct w1_slave *sl) 163{ 164 int ret; 165 int id; 166 struct platform_device *pdev; 167 168 id = new_bat_id(); 169 if (id < 0) { 170 ret = id; 171 goto noid; 172 } 173 174 pdev = platform_device_alloc("ds2780-battery", id); 175 if (!pdev) { 176 ret = -ENOMEM; 177 goto pdev_alloc_failed; 178 } 179 pdev->dev.parent = &sl->dev; 180 181 ret = platform_device_add(pdev); 182 if (ret) 183 goto pdev_add_failed; 184 185 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr); 186 if (ret) 187 goto bin_attr_failed; 188 189 dev_set_drvdata(&sl->dev, pdev); 190 191 return 0; 192 193bin_attr_failed: 194pdev_add_failed: 195 platform_device_unregister(pdev); 196pdev_alloc_failed: 197 release_bat_id(id); 198noid: 199 return ret; 200} 201 202static void w1_ds2780_remove_slave(struct w1_slave *sl) 203{ 204 struct platform_device *pdev = dev_get_drvdata(&sl->dev); 205 int id = pdev->id; 206 207 platform_device_unregister(pdev); 208 release_bat_id(id); 209 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr); 210} 211 212static struct w1_family_ops w1_ds2780_fops = { 213 .add_slave = w1_ds2780_add_slave, 214 .remove_slave = w1_ds2780_remove_slave, 215}; 216 217static struct w1_family w1_ds2780_family = { 218 .fid = W1_FAMILY_DS2780, 219 .fops = &w1_ds2780_fops, 220}; 221 222static int __init w1_ds2780_init(void) 223{ 224 idr_init(&bat_idr); 225 return w1_register_family(&w1_ds2780_family); 226} 227 228static void __exit w1_ds2780_exit(void) 229{ 230 w1_unregister_family(&w1_ds2780_family); 231 idr_destroy(&bat_idr); 232} 233 234module_init(w1_ds2780_init); 235module_exit(w1_ds2780_exit); 236 237MODULE_LICENSE("GPL"); 238MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>"); 239MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");