Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * LM73 Sensor driver |
| 3 | * Based on LM75 |
| 4 | * |
| 5 | * Copyright (C) 2007, CenoSYS (www.cenosys.com). |
| 6 | * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu). |
| 7 | * |
| 8 | * Guillaume Ligneul <guillaume.ligneul@gmail.com> |
| 9 | * Adrien Demarez <adrien.demarez@bolloretelecom.eu> |
| 10 | * Jeremy Laine <jeremy.laine@bolloretelecom.eu> |
| 11 | * |
| 12 | * This software program is licensed subject to the GNU General Public License |
| 13 | * (GPL).Version 2,June 1991, available at |
| 14 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 19 | #include <linux/i2c.h> |
| 20 | #include <linux/hwmon.h> |
| 21 | #include <linux/hwmon-sysfs.h> |
| 22 | #include <linux/err.h> |
| 23 | |
| 24 | |
| 25 | /* Addresses scanned */ |
| 26 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, |
| 27 | 0x4d, 0x4e, I2C_CLIENT_END }; |
| 28 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 29 | /* LM73 registers */ |
| 30 | #define LM73_REG_INPUT 0x00 |
| 31 | #define LM73_REG_CONF 0x01 |
| 32 | #define LM73_REG_MAX 0x02 |
| 33 | #define LM73_REG_MIN 0x03 |
| 34 | #define LM73_REG_CTRL 0x04 |
| 35 | #define LM73_REG_ID 0x07 |
| 36 | |
Jean Delvare | 90f4102 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 37 | #define LM73_ID 0x9001 /* 0x0190, byte-swapped */ |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 38 | #define DRVNAME "lm73" |
| 39 | #define LM73_TEMP_MIN (-40) |
| 40 | #define LM73_TEMP_MAX 150 |
| 41 | |
| 42 | /*-----------------------------------------------------------------------*/ |
| 43 | |
| 44 | |
| 45 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
| 46 | const char *buf, size_t count) |
| 47 | { |
| 48 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 49 | struct i2c_client *client = to_i2c_client(dev); |
| 50 | long temp; |
| 51 | short value; |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame^] | 52 | s32 err; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 53 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 54 | int status = kstrtol(buf, 10, &temp); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 55 | if (status < 0) |
| 56 | return status; |
| 57 | |
| 58 | /* Write value */ |
| 59 | value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4), |
| 60 | (LM73_TEMP_MAX*4)) << 5; |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame^] | 61 | err = i2c_smbus_write_word_swapped(client, attr->index, value); |
| 62 | return (err < 0) ? err : count; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
| 66 | char *buf) |
| 67 | { |
| 68 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 69 | struct i2c_client *client = to_i2c_client(dev); |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame^] | 70 | int temp; |
| 71 | |
| 72 | s32 err = i2c_smbus_read_word_swapped(client, attr->index); |
| 73 | if (err < 0) |
| 74 | return err; |
| 75 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 76 | /* use integer division instead of equivalent right shift to |
| 77 | guarantee arithmetic shift and preserve the sign */ |
Chris Verges | 0602934 | 2012-12-21 01:58:34 -0800 | [diff] [blame^] | 78 | temp = (((s16) err) * 250) / 32; |
| 79 | return scnprintf(buf, PAGE_SIZE, "%d\n", temp); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | /*-----------------------------------------------------------------------*/ |
| 84 | |
| 85 | /* sysfs attributes for hwmon */ |
| 86 | |
| 87 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 88 | show_temp, set_temp, LM73_REG_MAX); |
| 89 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, |
| 90 | show_temp, set_temp, LM73_REG_MIN); |
| 91 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, |
| 92 | show_temp, NULL, LM73_REG_INPUT); |
| 93 | |
| 94 | |
| 95 | static struct attribute *lm73_attributes[] = { |
| 96 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 97 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 98 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 99 | |
| 100 | NULL |
| 101 | }; |
| 102 | |
| 103 | static const struct attribute_group lm73_group = { |
| 104 | .attrs = lm73_attributes, |
| 105 | }; |
| 106 | |
| 107 | /*-----------------------------------------------------------------------*/ |
| 108 | |
| 109 | /* device probe and removal */ |
| 110 | |
| 111 | static int |
| 112 | lm73_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 113 | { |
| 114 | struct device *hwmon_dev; |
| 115 | int status; |
| 116 | |
| 117 | /* Register sysfs hooks */ |
| 118 | status = sysfs_create_group(&client->dev.kobj, &lm73_group); |
| 119 | if (status) |
| 120 | return status; |
| 121 | |
| 122 | hwmon_dev = hwmon_device_register(&client->dev); |
| 123 | if (IS_ERR(hwmon_dev)) { |
| 124 | status = PTR_ERR(hwmon_dev); |
| 125 | goto exit_remove; |
| 126 | } |
| 127 | i2c_set_clientdata(client, hwmon_dev); |
| 128 | |
| 129 | dev_info(&client->dev, "%s: sensor '%s'\n", |
| 130 | dev_name(hwmon_dev), client->name); |
| 131 | |
| 132 | return 0; |
| 133 | |
| 134 | exit_remove: |
| 135 | sysfs_remove_group(&client->dev.kobj, &lm73_group); |
| 136 | return status; |
| 137 | } |
| 138 | |
| 139 | static int lm73_remove(struct i2c_client *client) |
| 140 | { |
| 141 | struct device *hwmon_dev = i2c_get_clientdata(client); |
| 142 | |
| 143 | hwmon_device_unregister(hwmon_dev); |
| 144 | sysfs_remove_group(&client->dev.kobj, &lm73_group); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static const struct i2c_device_id lm73_ids[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 149 | { "lm73", 0 }, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 150 | { /* LIST END */ } |
| 151 | }; |
| 152 | MODULE_DEVICE_TABLE(i2c, lm73_ids); |
| 153 | |
| 154 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 155 | static int lm73_detect(struct i2c_client *new_client, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 156 | struct i2c_board_info *info) |
| 157 | { |
| 158 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 159 | int id, ctrl, conf; |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 160 | |
| 161 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 162 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 163 | return -ENODEV; |
| 164 | |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Do as much detection as possible with byte reads first, as word |
| 167 | * reads can confuse other devices. |
| 168 | */ |
| 169 | ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL); |
| 170 | if (ctrl < 0 || (ctrl & 0x10)) |
| 171 | return -ENODEV; |
| 172 | |
| 173 | conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF); |
| 174 | if (conf < 0 || (conf & 0x0c)) |
| 175 | return -ENODEV; |
| 176 | |
| 177 | id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID); |
| 178 | if (id < 0 || id != (LM73_ID & 0xff)) |
| 179 | return -ENODEV; |
| 180 | |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 181 | /* Check device ID */ |
| 182 | id = i2c_smbus_read_word_data(new_client, LM73_REG_ID); |
Jean Delvare | 24d6e2a | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 183 | if (id < 0 || id != LM73_ID) |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 184 | return -ENODEV; |
| 185 | |
| 186 | strlcpy(info->type, "lm73", I2C_NAME_SIZE); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static struct i2c_driver lm73_driver = { |
| 192 | .class = I2C_CLASS_HWMON, |
| 193 | .driver = { |
| 194 | .name = "lm73", |
| 195 | }, |
| 196 | .probe = lm73_probe, |
| 197 | .remove = lm73_remove, |
| 198 | .id_table = lm73_ids, |
| 199 | .detect = lm73_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 200 | .address_list = normal_i2c, |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 201 | }; |
| 202 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 203 | module_i2c_driver(lm73_driver); |
Adrien Demarez | 4e233cb | 2009-12-09 20:35:50 +0100 | [diff] [blame] | 204 | |
| 205 | MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>"); |
| 206 | MODULE_DESCRIPTION("LM73 driver"); |
| 207 | MODULE_LICENSE("GPL"); |