Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net> |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 3 | Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | the help of Jean Delvare <khali@linux-fr.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/i2c.h> |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Jean Delvare | 4275fcd | 2010-10-28 20:31:49 +0200 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/hwmon.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* Insmod parameters */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | static int input_mode; |
| 32 | module_param(input_mode, int, 0); |
| 33 | MODULE_PARM_DESC(input_mode, |
| 34 | "Analog input mode:\n" |
| 35 | " 0 = four single ended inputs\n" |
| 36 | " 1 = three differential inputs\n" |
| 37 | " 2 = single ended and differential mixed\n" |
| 38 | " 3 = two differential inputs\n"); |
| 39 | |
| 40 | /* The PCF8591 control byte |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 41 | 7 6 5 4 3 2 1 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | | 0 |AOEF| AIP | 0 |AINC| AICH | */ |
| 43 | |
| 44 | /* Analog Output Enable Flag (analog output active if 1) */ |
| 45 | #define PCF8591_CONTROL_AOEF 0x40 |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 46 | |
| 47 | /* Analog Input Programming |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 0x00 = four single ended inputs |
| 49 | 0x10 = three differential inputs |
| 50 | 0x20 = single ended and differential mixed |
| 51 | 0x30 = two differential inputs */ |
| 52 | #define PCF8591_CONTROL_AIP_MASK 0x30 |
| 53 | |
| 54 | /* Autoincrement Flag (switch on if 1) */ |
| 55 | #define PCF8591_CONTROL_AINC 0x04 |
| 56 | |
| 57 | /* Channel selection |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 58 | 0x00 = channel 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 0x01 = channel 1 |
| 60 | 0x02 = channel 2 |
| 61 | 0x03 = channel 3 */ |
| 62 | #define PCF8591_CONTROL_AICH_MASK 0x03 |
| 63 | |
| 64 | /* Initial values */ |
| 65 | #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF) |
| 66 | #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */ |
| 67 | |
| 68 | /* Conversions */ |
| 69 | #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg)) |
| 70 | |
| 71 | struct pcf8591_data { |
Jean Delvare | 4275fcd | 2010-10-28 20:31:49 +0200 | [diff] [blame] | 72 | struct device *hwmon_dev; |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 73 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | u8 control; |
| 76 | u8 aout; |
| 77 | }; |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static void pcf8591_init_client(struct i2c_client *client); |
| 80 | static int pcf8591_read_channel(struct device *dev, int channel); |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* following are the sysfs callback functions */ |
| 83 | #define show_in_channel(channel) \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 84 | static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { \ |
| 86 | return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\ |
| 87 | } \ |
| 88 | static DEVICE_ATTR(in##channel##_input, S_IRUGO, \ |
| 89 | show_in##channel##_input, NULL); |
| 90 | |
| 91 | show_in_channel(0); |
| 92 | show_in_channel(1); |
| 93 | show_in_channel(2); |
| 94 | show_in_channel(3); |
| 95 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 96 | static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev)); |
| 99 | return sprintf(buf, "%d\n", data->aout * 10); |
| 100 | } |
| 101 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 102 | static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | unsigned int value; |
| 105 | struct i2c_client *client = to_i2c_client(dev); |
| 106 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 107 | if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) { |
| 108 | data->aout = value; |
| 109 | i2c_smbus_write_byte_data(client, data->control, data->aout); |
| 110 | return count; |
| 111 | } |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 115 | static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | show_out0_ouput, set_out0_output); |
| 117 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 118 | static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev)); |
| 121 | return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF))); |
| 122 | } |
| 123 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 124 | static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | struct i2c_client *client = to_i2c_client(dev); |
| 127 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 128 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 129 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 130 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (val) |
| 132 | data->control |= PCF8591_CONTROL_AOEF; |
| 133 | else |
| 134 | data->control &= ~PCF8591_CONTROL_AOEF; |
| 135 | i2c_smbus_write_byte(client, data->control); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 136 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return count; |
| 138 | } |
| 139 | |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 140 | static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | show_out0_enable, set_out0_enable); |
| 142 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 143 | static struct attribute *pcf8591_attributes[] = { |
| 144 | &dev_attr_out0_enable.attr, |
| 145 | &dev_attr_out0_output.attr, |
| 146 | &dev_attr_in0_input.attr, |
| 147 | &dev_attr_in1_input.attr, |
| 148 | NULL |
| 149 | }; |
| 150 | |
| 151 | static const struct attribute_group pcf8591_attr_group = { |
| 152 | .attrs = pcf8591_attributes, |
| 153 | }; |
| 154 | |
| 155 | static struct attribute *pcf8591_attributes_opt[] = { |
| 156 | &dev_attr_in2_input.attr, |
| 157 | &dev_attr_in3_input.attr, |
| 158 | NULL |
| 159 | }; |
| 160 | |
| 161 | static const struct attribute_group pcf8591_attr_group_opt = { |
| 162 | .attrs = pcf8591_attributes_opt, |
| 163 | }; |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Real code |
| 167 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 169 | static int pcf8591_probe(struct i2c_client *client, |
| 170 | const struct i2c_device_id *id) |
| 171 | { |
| 172 | struct pcf8591_data *data; |
| 173 | int err; |
| 174 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 175 | if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | err = -ENOMEM; |
| 177 | goto exit; |
| 178 | } |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 179 | |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 180 | i2c_set_clientdata(client, data); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 181 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* Initialize the PCF8591 chip */ |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 184 | pcf8591_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* Register sysfs hooks */ |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 187 | err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group); |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 188 | if (err) |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 189 | goto exit_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | /* Register input2 if not in "two differential inputs" mode */ |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 192 | if (input_mode != 3) { |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 193 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 194 | &dev_attr_in2_input))) |
| 195 | goto exit_sysfs_remove; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 198 | /* Register input3 only in "four single ended inputs" mode */ |
| 199 | if (input_mode == 0) { |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 200 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 201 | &dev_attr_in3_input))) |
| 202 | goto exit_sysfs_remove; |
| 203 | } |
| 204 | |
Jean Delvare | 4275fcd | 2010-10-28 20:31:49 +0200 | [diff] [blame] | 205 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 206 | if (IS_ERR(data->hwmon_dev)) { |
| 207 | err = PTR_ERR(data->hwmon_dev); |
| 208 | goto exit_sysfs_remove; |
| 209 | } |
| 210 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 211 | return 0; |
| 212 | |
| 213 | exit_sysfs_remove: |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 214 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt); |
| 215 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | exit_kfree: |
| 217 | kfree(data); |
| 218 | exit: |
| 219 | return err; |
| 220 | } |
| 221 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 222 | static int pcf8591_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Jean Delvare | 4275fcd | 2010-10-28 20:31:49 +0200 | [diff] [blame] | 224 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 225 | |
| 226 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 227 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt); |
| 228 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | kfree(i2c_get_clientdata(client)); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* Called when we have found a new PCF8591. */ |
| 234 | static void pcf8591_init_client(struct i2c_client *client) |
| 235 | { |
| 236 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 237 | data->control = PCF8591_INIT_CONTROL; |
| 238 | data->aout = PCF8591_INIT_AOUT; |
| 239 | |
| 240 | i2c_smbus_write_byte_data(client, data->control, data->aout); |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 241 | |
| 242 | /* The first byte transmitted contains the conversion code of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | previous read cycle. FLUSH IT! */ |
| 244 | i2c_smbus_read_byte(client); |
| 245 | } |
| 246 | |
| 247 | static int pcf8591_read_channel(struct device *dev, int channel) |
| 248 | { |
| 249 | u8 value; |
| 250 | struct i2c_client *client = to_i2c_client(dev); |
| 251 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 252 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 253 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) { |
| 256 | data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK) |
| 257 | | channel; |
| 258 | i2c_smbus_write_byte(client, data->control); |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 259 | |
| 260 | /* The first byte transmitted contains the conversion code of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | the previous read cycle. FLUSH IT! */ |
| 262 | i2c_smbus_read_byte(client); |
| 263 | } |
| 264 | value = i2c_smbus_read_byte(client); |
| 265 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 266 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if ((channel == 2 && input_mode == 2) || |
| 269 | (channel != 3 && (input_mode == 1 || input_mode == 3))) |
| 270 | return (10 * REG_TO_SIGNED(value)); |
| 271 | else |
| 272 | return (10 * value); |
| 273 | } |
| 274 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 275 | static const struct i2c_device_id pcf8591_id[] = { |
| 276 | { "pcf8591", 0 }, |
| 277 | { } |
| 278 | }; |
| 279 | MODULE_DEVICE_TABLE(i2c, pcf8591_id); |
| 280 | |
| 281 | static struct i2c_driver pcf8591_driver = { |
| 282 | .driver = { |
| 283 | .name = "pcf8591", |
| 284 | }, |
| 285 | .probe = pcf8591_probe, |
| 286 | .remove = pcf8591_remove, |
| 287 | .id_table = pcf8591_id, |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 288 | }; |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | static int __init pcf8591_init(void) |
| 291 | { |
| 292 | if (input_mode < 0 || input_mode > 3) { |
| 293 | printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n", |
| 294 | input_mode); |
| 295 | input_mode = 0; |
| 296 | } |
| 297 | return i2c_add_driver(&pcf8591_driver); |
| 298 | } |
| 299 | |
| 300 | static void __exit pcf8591_exit(void) |
| 301 | { |
| 302 | i2c_del_driver(&pcf8591_driver); |
| 303 | } |
| 304 | |
| 305 | MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>"); |
| 306 | MODULE_DESCRIPTION("PCF8591 driver"); |
| 307 | MODULE_LICENSE("GPL"); |
| 308 | |
| 309 | module_init(pcf8591_init); |
| 310 | module_exit(pcf8591_exit); |