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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* Addresses to scan */ |
Jean Delvare | 2cdddeb | 2008-01-27 18:14:47 +0100 | [diff] [blame] | 28 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | /* Insmod parameters */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | static int input_mode; |
| 34 | module_param(input_mode, int, 0); |
| 35 | MODULE_PARM_DESC(input_mode, |
| 36 | "Analog input mode:\n" |
| 37 | " 0 = four single ended inputs\n" |
| 38 | " 1 = three differential inputs\n" |
| 39 | " 2 = single ended and differential mixed\n" |
| 40 | " 3 = two differential inputs\n"); |
| 41 | |
| 42 | /* The PCF8591 control byte |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 43 | 7 6 5 4 3 2 1 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | | 0 |AOEF| AIP | 0 |AINC| AICH | */ |
| 45 | |
| 46 | /* Analog Output Enable Flag (analog output active if 1) */ |
| 47 | #define PCF8591_CONTROL_AOEF 0x40 |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 48 | |
| 49 | /* Analog Input Programming |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 0x00 = four single ended inputs |
| 51 | 0x10 = three differential inputs |
| 52 | 0x20 = single ended and differential mixed |
| 53 | 0x30 = two differential inputs */ |
| 54 | #define PCF8591_CONTROL_AIP_MASK 0x30 |
| 55 | |
| 56 | /* Autoincrement Flag (switch on if 1) */ |
| 57 | #define PCF8591_CONTROL_AINC 0x04 |
| 58 | |
| 59 | /* Channel selection |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 60 | 0x00 = channel 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | 0x01 = channel 1 |
| 62 | 0x02 = channel 2 |
| 63 | 0x03 = channel 3 */ |
| 64 | #define PCF8591_CONTROL_AICH_MASK 0x03 |
| 65 | |
| 66 | /* Initial values */ |
| 67 | #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF) |
| 68 | #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */ |
| 69 | |
| 70 | /* Conversions */ |
| 71 | #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg)) |
| 72 | |
| 73 | struct pcf8591_data { |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 74 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | u8 control; |
| 77 | u8 aout; |
| 78 | }; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static void pcf8591_init_client(struct i2c_client *client); |
| 81 | static int pcf8591_read_channel(struct device *dev, int channel); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* following are the sysfs callback functions */ |
| 84 | #define show_in_channel(channel) \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 85 | 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] | 86 | { \ |
| 87 | return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\ |
| 88 | } \ |
| 89 | static DEVICE_ATTR(in##channel##_input, S_IRUGO, \ |
| 90 | show_in##channel##_input, NULL); |
| 91 | |
| 92 | show_in_channel(0); |
| 93 | show_in_channel(1); |
| 94 | show_in_channel(2); |
| 95 | show_in_channel(3); |
| 96 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 97 | 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] | 98 | { |
| 99 | struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev)); |
| 100 | return sprintf(buf, "%d\n", data->aout * 10); |
| 101 | } |
| 102 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | unsigned int value; |
| 106 | struct i2c_client *client = to_i2c_client(dev); |
| 107 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 108 | if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) { |
| 109 | data->aout = value; |
| 110 | i2c_smbus_write_byte_data(client, data->control, data->aout); |
| 111 | return count; |
| 112 | } |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 116 | static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | show_out0_ouput, set_out0_output); |
| 118 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 119 | 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] | 120 | { |
| 121 | struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev)); |
| 122 | return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF))); |
| 123 | } |
| 124 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 125 | 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] | 126 | { |
| 127 | struct i2c_client *client = to_i2c_client(dev); |
| 128 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 129 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 130 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 131 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (val) |
| 133 | data->control |= PCF8591_CONTROL_AOEF; |
| 134 | else |
| 135 | data->control &= ~PCF8591_CONTROL_AOEF; |
| 136 | i2c_smbus_write_byte(client, data->control); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 137 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return count; |
| 139 | } |
| 140 | |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 141 | static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | show_out0_enable, set_out0_enable); |
| 143 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 144 | static struct attribute *pcf8591_attributes[] = { |
| 145 | &dev_attr_out0_enable.attr, |
| 146 | &dev_attr_out0_output.attr, |
| 147 | &dev_attr_in0_input.attr, |
| 148 | &dev_attr_in1_input.attr, |
| 149 | NULL |
| 150 | }; |
| 151 | |
| 152 | static const struct attribute_group pcf8591_attr_group = { |
| 153 | .attrs = pcf8591_attributes, |
| 154 | }; |
| 155 | |
| 156 | static struct attribute *pcf8591_attributes_opt[] = { |
| 157 | &dev_attr_in2_input.attr, |
| 158 | &dev_attr_in3_input.attr, |
| 159 | NULL |
| 160 | }; |
| 161 | |
| 162 | static const struct attribute_group pcf8591_attr_group_opt = { |
| 163 | .attrs = pcf8591_attributes_opt, |
| 164 | }; |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Real code |
| 168 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 170 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 171 | static int pcf8591_detect(struct i2c_client *client, |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 172 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 174 | struct i2c_adapter *adapter = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE |
| 177 | | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 178 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 180 | /* Now, we would do the remaining detection. But the PCF8591 is plainly |
| 181 | impossible to detect! Stupid chip. */ |
| 182 | |
| 183 | strlcpy(info->type, "pcf8591", I2C_NAME_SIZE); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int pcf8591_probe(struct i2c_client *client, |
| 189 | const struct i2c_device_id *id) |
| 190 | { |
| 191 | struct pcf8591_data *data; |
| 192 | int err; |
| 193 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 194 | if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | err = -ENOMEM; |
| 196 | goto exit; |
| 197 | } |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 198 | |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 199 | i2c_set_clientdata(client, data); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 200 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* Initialize the PCF8591 chip */ |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 203 | pcf8591_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | /* Register sysfs hooks */ |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 206 | err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group); |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 207 | if (err) |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 208 | goto exit_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* Register input2 if not in "two differential inputs" mode */ |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 211 | if (input_mode != 3) { |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 212 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 213 | &dev_attr_in2_input))) |
| 214 | goto exit_sysfs_remove; |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 217 | /* Register input3 only in "four single ended inputs" mode */ |
| 218 | if (input_mode == 0) { |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 219 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 220 | &dev_attr_in3_input))) |
| 221 | goto exit_sysfs_remove; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | exit_sysfs_remove: |
Jean Delvare | f741f67 | 2008-07-14 22:38:36 +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 | exit_kfree: |
| 230 | kfree(data); |
| 231 | exit: |
| 232 | return err; |
| 233 | } |
| 234 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 235 | static int pcf8591_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 237 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt); |
| 238 | sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | kfree(i2c_get_clientdata(client)); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* Called when we have found a new PCF8591. */ |
| 244 | static void pcf8591_init_client(struct i2c_client *client) |
| 245 | { |
| 246 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 247 | data->control = PCF8591_INIT_CONTROL; |
| 248 | data->aout = PCF8591_INIT_AOUT; |
| 249 | |
| 250 | i2c_smbus_write_byte_data(client, data->control, data->aout); |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 251 | |
| 252 | /* The first byte transmitted contains the conversion code of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | previous read cycle. FLUSH IT! */ |
| 254 | i2c_smbus_read_byte(client); |
| 255 | } |
| 256 | |
| 257 | static int pcf8591_read_channel(struct device *dev, int channel) |
| 258 | { |
| 259 | u8 value; |
| 260 | struct i2c_client *client = to_i2c_client(dev); |
| 261 | struct pcf8591_data *data = i2c_get_clientdata(client); |
| 262 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 263 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) { |
| 266 | data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK) |
| 267 | | channel; |
| 268 | i2c_smbus_write_byte(client, data->control); |
Jean Delvare | fb4504f | 2009-03-30 21:46:43 +0200 | [diff] [blame] | 269 | |
| 270 | /* The first byte transmitted contains the conversion code of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | the previous read cycle. FLUSH IT! */ |
| 272 | i2c_smbus_read_byte(client); |
| 273 | } |
| 274 | value = i2c_smbus_read_byte(client); |
| 275 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 276 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | if ((channel == 2 && input_mode == 2) || |
| 279 | (channel != 3 && (input_mode == 1 || input_mode == 3))) |
| 280 | return (10 * REG_TO_SIGNED(value)); |
| 281 | else |
| 282 | return (10 * value); |
| 283 | } |
| 284 | |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 285 | static const struct i2c_device_id pcf8591_id[] = { |
| 286 | { "pcf8591", 0 }, |
| 287 | { } |
| 288 | }; |
| 289 | MODULE_DEVICE_TABLE(i2c, pcf8591_id); |
| 290 | |
| 291 | static struct i2c_driver pcf8591_driver = { |
| 292 | .driver = { |
| 293 | .name = "pcf8591", |
| 294 | }, |
| 295 | .probe = pcf8591_probe, |
| 296 | .remove = pcf8591_remove, |
| 297 | .id_table = pcf8591_id, |
| 298 | |
| 299 | .class = I2C_CLASS_HWMON, /* Nearest choice */ |
| 300 | .detect = pcf8591_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 301 | .address_list = normal_i2c, |
Jean Delvare | 8b77e6a | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 302 | }; |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | static int __init pcf8591_init(void) |
| 305 | { |
| 306 | if (input_mode < 0 || input_mode > 3) { |
| 307 | printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n", |
| 308 | input_mode); |
| 309 | input_mode = 0; |
| 310 | } |
| 311 | return i2c_add_driver(&pcf8591_driver); |
| 312 | } |
| 313 | |
| 314 | static void __exit pcf8591_exit(void) |
| 315 | { |
| 316 | i2c_del_driver(&pcf8591_driver); |
| 317 | } |
| 318 | |
| 319 | MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>"); |
| 320 | MODULE_DESCRIPTION("PCF8591 driver"); |
| 321 | MODULE_LICENSE("GPL"); |
| 322 | |
| 323 | module_init(pcf8591_init); |
| 324 | module_exit(pcf8591_exit); |