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