blob: 62e19131113942ab797cd8f0c7658102758aff98 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck8c103692012-01-14 13:20:00 -08002 * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 *
5 * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6 *
7 * (derived from w83781d.c)
8 *
9 * Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
10 * Philip Edelbrock <phil@netroedge.com>, and
11 * Mark Studebaker <mdsxyz123@yahoo.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
Guenter Roeck8c103692012-01-14 13:20:00 -080029 * This driver supports the hardware sensor chips: Asus ASB100 and
30 * ASB100-A "BACH".
31 *
32 * ASB100-A supports pwm1, while plain ASB100 does not. There is no known
33 * way for the driver to tell which one is there.
34 *
Jean Delvare4101ece2012-11-05 21:54:40 +010035 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeck8c103692012-01-14 13:20:00 -080036 * asb100 7 3 1 4 0x31 0x0694 yes no
37 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Joe Perches4d630e22010-10-20 06:51:29 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/slab.h>
43#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/hwmon.h>
Jean Delvarefad33c52008-01-03 23:21:07 +010045#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020046#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020049#include <linux/jiffies.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010050#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "lm75.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* I2C addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050054static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jean Delvare3aed1982009-01-07 16:37:32 +010056static unsigned short force_subclients[4];
57module_param_array(force_subclients, short, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080058MODULE_PARM_DESC(force_subclients,
59 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* Voltage IN registers 0-6 */
62#define ASB100_REG_IN(nr) (0x20 + (nr))
63#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
64#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
65
66/* FAN IN registers 1-3 */
67#define ASB100_REG_FAN(nr) (0x28 + (nr))
68#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
69
70/* TEMPERATURE registers 1-4 */
71static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
72static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
73static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
74
75#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
76#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
77#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
78
79#define ASB100_REG_TEMP2_CONFIG 0x0152
80#define ASB100_REG_TEMP3_CONFIG 0x0252
81
82
83#define ASB100_REG_CONFIG 0x40
84#define ASB100_REG_ALARM1 0x41
85#define ASB100_REG_ALARM2 0x42
86#define ASB100_REG_SMIM1 0x43
87#define ASB100_REG_SMIM2 0x44
88#define ASB100_REG_VID_FANDIV 0x47
89#define ASB100_REG_I2C_ADDR 0x48
90#define ASB100_REG_CHIPID 0x49
91#define ASB100_REG_I2C_SUBADDR 0x4a
92#define ASB100_REG_PIN 0x4b
93#define ASB100_REG_IRQ 0x4c
94#define ASB100_REG_BANK 0x4e
95#define ASB100_REG_CHIPMAN 0x4f
96
97#define ASB100_REG_WCHIPID 0x58
98
99/* bit 7 -> enable, bits 0-3 -> duty cycle */
100#define ASB100_REG_PWM1 0x59
101
Guenter Roeck8c103692012-01-14 13:20:00 -0800102/*
103 * CONVERSIONS
104 * Rounding and limit checking is only done on the TO_REG variants.
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* These constants are a guess, consistent w/ w83781d */
Guenter Roeck8c103692012-01-14 13:20:00 -0800108#define ASB100_IN_MIN 0
109#define ASB100_IN_MAX 4080
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Guenter Roeck8c103692012-01-14 13:20:00 -0800111/*
112 * IN: 1/1000 V (0V to 4.08V)
113 * REG: 16mV/bit
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static u8 IN_TO_REG(unsigned val)
116{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800117 unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return (nval + 8) / 16;
119}
120
121static unsigned IN_FROM_REG(u8 reg)
122{
123 return reg * 16;
124}
125
126static u8 FAN_TO_REG(long rpm, int div)
127{
128 if (rpm == -1)
129 return 0;
130 if (rpm == 0)
131 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800132 rpm = clamp_val(rpm, 1, 1000000);
133 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static int FAN_FROM_REG(u8 val, int div)
137{
Guenter Roeck8c103692012-01-14 13:20:00 -0800138 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141/* These constants are a guess, consistent w/ w83781d */
Guenter Roeck8c103692012-01-14 13:20:00 -0800142#define ASB100_TEMP_MIN -128000
143#define ASB100_TEMP_MAX 127000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Guenter Roeck8c103692012-01-14 13:20:00 -0800145/*
146 * TEMP: 0.001C/bit (-128C to +127C)
147 * REG: 1C/bit, two's complement
148 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200149static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800151 int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
Guenter Roeck8c103692012-01-14 13:20:00 -0800152 ntemp += (ntemp < 0 ? -500 : 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return (u8)(ntemp / 1000);
154}
155
156static int TEMP_FROM_REG(u8 reg)
157{
158 return (s8)reg * 1000;
159}
160
Guenter Roeck8c103692012-01-14 13:20:00 -0800161/*
162 * PWM: 0 - 255 per sensors documentation
163 * REG: (6.25% duty cycle per bit)
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static u8 ASB100_PWM_TO_REG(int pwm)
166{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800167 pwm = clamp_val(pwm, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return (u8)(pwm / 16);
169}
170
171static int ASB100_PWM_FROM_REG(u8 reg)
172{
173 return reg * 16;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define DIV_FROM_REG(val) (1 << (val))
177
Guenter Roeck8c103692012-01-14 13:20:00 -0800178/*
179 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
180 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static u8 DIV_TO_REG(long val)
183{
Guenter Roeck8c103692012-01-14 13:20:00 -0800184 return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Guenter Roeck8c103692012-01-14 13:20:00 -0800187/*
188 * For each registered client, we need to keep some data in memory. That
189 * data is pointed to by client->data. The structure itself is
190 * dynamically allocated, at the same time the client itself is allocated.
191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192struct asb100_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700193 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100194 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100196 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long last_updated; /* In jiffies */
198
199 /* array of 2 pointers to subclients */
200 struct i2c_client *lm75[2];
201
202 char valid; /* !=0 if following fields are valid */
203 u8 in[7]; /* Register value */
204 u8 in_max[7]; /* Register value */
205 u8 in_min[7]; /* Register value */
206 u8 fan[3]; /* Register value */
207 u8 fan_min[3]; /* Register value */
208 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
209 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
210 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
211 u8 fan_div[3]; /* Register encoding, right justified */
212 u8 pwm; /* Register encoding */
213 u8 vid; /* Register encoding, combined */
214 u32 alarms; /* Register encoding, combined */
215 u8 vrm;
216};
217
218static int asb100_read_value(struct i2c_client *client, u16 reg);
219static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
220
Jean Delvare063675b2008-07-16 19:30:11 +0200221static int asb100_probe(struct i2c_client *client,
222 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100223static int asb100_detect(struct i2c_client *client,
Jean Delvare063675b2008-07-16 19:30:11 +0200224 struct i2c_board_info *info);
225static int asb100_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static struct asb100_data *asb100_update_device(struct device *dev);
227static void asb100_init_client(struct i2c_client *client);
228
Jean Delvare063675b2008-07-16 19:30:11 +0200229static const struct i2c_device_id asb100_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100230 { "asb100", 0 },
Jean Delvare063675b2008-07-16 19:30:11 +0200231 { }
232};
233MODULE_DEVICE_TABLE(i2c, asb100_id);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static struct i2c_driver asb100_driver = {
Jean Delvare063675b2008-07-16 19:30:11 +0200236 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100237 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100238 .name = "asb100",
239 },
Jean Delvare063675b2008-07-16 19:30:11 +0200240 .probe = asb100_probe,
241 .remove = asb100_remove,
242 .id_table = asb100_id,
243 .detect = asb100_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100244 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
247/* 7 Voltages */
248#define show_in_reg(reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100249static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
250 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100252 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct asb100_data *data = asb100_update_device(dev); \
254 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
255}
256
257show_in_reg(in)
258show_in_reg(in_min)
259show_in_reg(in_max)
260
261#define set_in_reg(REG, reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100262static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
263 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100265 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct i2c_client *client = to_i2c_client(dev); \
267 struct asb100_data *data = i2c_get_clientdata(client); \
Guenter Roeck8c103692012-01-14 13:20:00 -0800268 unsigned long val; \
269 int err = kstrtoul(buf, 10, &val); \
270 if (err) \
271 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100272 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 data->in_##reg[nr] = IN_TO_REG(val); \
274 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
275 data->in_##reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100276 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return count; \
278}
279
280set_in_reg(MIN, min)
281set_in_reg(MAX, max)
282
283#define sysfs_in(offset) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100284static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
285 show_in, NULL, offset); \
286static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
287 show_in_min, set_in_min, offset); \
288static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
289 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291sysfs_in(0);
292sysfs_in(1);
293sysfs_in(2);
294sysfs_in(3);
295sysfs_in(4);
296sysfs_in(5);
297sysfs_in(6);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/* 3 Fans */
Jean Delvarefad33c52008-01-03 23:21:07 +0100300static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
301 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jean Delvarefad33c52008-01-03 23:21:07 +0100303 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct asb100_data *data = asb100_update_device(dev);
305 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
306 DIV_FROM_REG(data->fan_div[nr])));
307}
308
Jean Delvarefad33c52008-01-03 23:21:07 +0100309static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
310 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jean Delvarefad33c52008-01-03 23:21:07 +0100312 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct asb100_data *data = asb100_update_device(dev);
314 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
315 DIV_FROM_REG(data->fan_div[nr])));
316}
317
Jean Delvarefad33c52008-01-03 23:21:07 +0100318static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
319 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Jean Delvarefad33c52008-01-03 23:21:07 +0100321 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct asb100_data *data = asb100_update_device(dev);
323 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
324}
325
Jean Delvarefad33c52008-01-03 23:21:07 +0100326static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Jean Delvarefad33c52008-01-03 23:21:07 +0100329 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct i2c_client *client = to_i2c_client(dev);
331 struct asb100_data *data = i2c_get_clientdata(client);
Guenter Roeck8c103692012-01-14 13:20:00 -0800332 unsigned long val;
333 int err;
334
335 err = kstrtoul(buf, 10, &val);
336 if (err)
337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
341 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100342 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return count;
344}
345
Guenter Roeck8c103692012-01-14 13:20:00 -0800346/*
347 * Note: we save and restore the fan minimum here, because its value is
348 * determined in part by the fan divisor. This follows the principle of
349 * least surprise; the user doesn't expect the fan minimum to change just
350 * because the divisor changed.
351 */
Jean Delvarefad33c52008-01-03 23:21:07 +0100352static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
353 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jean Delvarefad33c52008-01-03 23:21:07 +0100355 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct i2c_client *client = to_i2c_client(dev);
357 struct asb100_data *data = i2c_get_clientdata(client);
358 unsigned long min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int reg;
Guenter Roeck8c103692012-01-14 13:20:00 -0800360 unsigned long val;
361 int err;
362
363 err = kstrtoul(buf, 10, &val);
364 if (err)
365 return err;
Jean Delvareaf2219312008-01-03 23:15:49 +0100366
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100367 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 min = FAN_FROM_REG(data->fan_min[nr],
370 DIV_FROM_REG(data->fan_div[nr]));
371 data->fan_div[nr] = DIV_TO_REG(val);
372
Jean Delvareaf2219312008-01-03 23:15:49 +0100373 switch (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 case 0: /* fan 1 */
375 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
376 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
377 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
378 break;
379
380 case 1: /* fan 2 */
381 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
382 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
383 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
384 break;
385
386 case 2: /* fan 3 */
387 reg = asb100_read_value(client, ASB100_REG_PIN);
388 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
389 asb100_write_value(client, ASB100_REG_PIN, reg);
390 break;
391 }
392
393 data->fan_min[nr] =
394 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
395 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
396
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100397 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return count;
400}
401
402#define sysfs_fan(offset) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100403static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
404 show_fan, NULL, offset - 1); \
405static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
406 show_fan_min, set_fan_min, offset - 1); \
407static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
408 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410sysfs_fan(1);
411sysfs_fan(2);
412sysfs_fan(3);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* 4 Temp. Sensors */
415static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
416{
417 int ret = 0;
418
419 switch (nr) {
420 case 1: case 2:
421 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
422 break;
423 case 0: case 3: default:
424 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
425 break;
426 }
427 return ret;
428}
Jean Delvareaf2219312008-01-03 23:15:49 +0100429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#define show_temp_reg(reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100431static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
432 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100434 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct asb100_data *data = asb100_update_device(dev); \
436 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
437}
438
439show_temp_reg(temp);
440show_temp_reg(temp_max);
441show_temp_reg(temp_hyst);
442
443#define set_temp_reg(REG, reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100444static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
445 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100447 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct i2c_client *client = to_i2c_client(dev); \
449 struct asb100_data *data = i2c_get_clientdata(client); \
Guenter Roeck8c103692012-01-14 13:20:00 -0800450 long val; \
451 int err = kstrtol(buf, 10, &val); \
452 if (err) \
453 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100454 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 switch (nr) { \
456 case 1: case 2: \
457 data->reg[nr] = LM75_TEMP_TO_REG(val); \
458 break; \
459 case 0: case 3: default: \
460 data->reg[nr] = TEMP_TO_REG(val); \
461 break; \
462 } \
463 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
464 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100465 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return count; \
467}
468
469set_temp_reg(MAX, temp_max);
470set_temp_reg(HYST, temp_hyst);
471
472#define sysfs_temp(num) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100473static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
474 show_temp, NULL, num - 1); \
475static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
476 show_temp_max, set_temp_max, num - 1); \
477static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
478 show_temp_hyst, set_temp_hyst, num - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480sysfs_temp(1);
481sysfs_temp(2);
482sysfs_temp(3);
483sysfs_temp(4);
484
485/* VID */
Julia Lawall82e73f72016-12-22 13:04:36 +0100486static ssize_t cpu0_vid_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct asb100_data *data = asb100_update_device(dev);
490 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
491}
492
Julia Lawall82e73f72016-12-22 13:04:36 +0100493static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/* VRM */
Julia Lawall82e73f72016-12-22 13:04:36 +0100496static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
Jean Delvareaf2219312008-01-03 23:15:49 +0100497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jean Delvare90d66192007-10-08 18:24:35 +0200499 struct asb100_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return sprintf(buf, "%d\n", data->vrm);
501}
502
Julia Lawall82e73f72016-12-22 13:04:36 +0100503static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
504 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100506 struct asb100_data *data = dev_get_drvdata(dev);
Guenter Roeck8c103692012-01-14 13:20:00 -0800507 unsigned long val;
508 int err;
509
510 err = kstrtoul(buf, 10, &val);
511 if (err)
512 return err;
Axel Lindb59ac42014-08-06 08:21:49 +0800513
514 if (val > 255)
515 return -EINVAL;
516
Guenter Roeck8c103692012-01-14 13:20:00 -0800517 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return count;
519}
520
521/* Alarms */
Julia Lawall82e73f72016-12-22 13:04:36 +0100522static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Julia Lawall82e73f72016-12-22 13:04:36 +0100524static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Jean Delvareaf2219312008-01-03 23:15:49 +0100525 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct asb100_data *data = asb100_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200528 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Julia Lawall82e73f72016-12-22 13:04:36 +0100531static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jean Delvare636866b2008-01-03 23:24:24 +0100533static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
534 char *buf)
535{
536 int bitnr = to_sensor_dev_attr(attr)->index;
537 struct asb100_data *data = asb100_update_device(dev);
538 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
539}
540static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
541static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
542static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
543static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
544static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
545static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
546static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
547static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
548static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
549static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
550static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/* 1 PWM */
Julia Lawall82e73f72016-12-22 13:04:36 +0100553static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
Jean Delvareaf2219312008-01-03 23:15:49 +0100554 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct asb100_data *data = asb100_update_device(dev);
557 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
558}
559
Julia Lawall82e73f72016-12-22 13:04:36 +0100560static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
561 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct i2c_client *client = to_i2c_client(dev);
564 struct asb100_data *data = i2c_get_clientdata(client);
Guenter Roeck8c103692012-01-14 13:20:00 -0800565 unsigned long val;
566 int err;
567
568 err = kstrtoul(buf, 10, &val);
569 if (err)
570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100572 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 data->pwm &= 0x80; /* keep the enable bit */
574 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
575 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100576 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return count;
578}
579
Julia Lawall82e73f72016-12-22 13:04:36 +0100580static ssize_t pwm1_enable_show(struct device *dev,
Jean Delvareaf2219312008-01-03 23:15:49 +0100581 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct asb100_data *data = asb100_update_device(dev);
584 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
585}
586
Julia Lawall82e73f72016-12-22 13:04:36 +0100587static ssize_t pwm1_enable_store(struct device *dev,
588 struct device_attribute *attr,
589 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct i2c_client *client = to_i2c_client(dev);
592 struct asb100_data *data = i2c_get_clientdata(client);
Guenter Roeck8c103692012-01-14 13:20:00 -0800593 unsigned long val;
594 int err;
595
596 err = kstrtoul(buf, 10, &val);
597 if (err)
598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100600 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 data->pwm &= 0x0f; /* keep the duty cycle bits */
602 data->pwm |= (val ? 0x80 : 0x00);
603 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100604 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return count;
606}
607
Julia Lawall82e73f72016-12-22 13:04:36 +0100608static DEVICE_ATTR_RW(pwm1);
609static DEVICE_ATTR_RW(pwm1_enable);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200610
611static struct attribute *asb100_attributes[] = {
Jean Delvarefad33c52008-01-03 23:21:07 +0100612 &sensor_dev_attr_in0_input.dev_attr.attr,
613 &sensor_dev_attr_in0_min.dev_attr.attr,
614 &sensor_dev_attr_in0_max.dev_attr.attr,
615 &sensor_dev_attr_in1_input.dev_attr.attr,
616 &sensor_dev_attr_in1_min.dev_attr.attr,
617 &sensor_dev_attr_in1_max.dev_attr.attr,
618 &sensor_dev_attr_in2_input.dev_attr.attr,
619 &sensor_dev_attr_in2_min.dev_attr.attr,
620 &sensor_dev_attr_in2_max.dev_attr.attr,
621 &sensor_dev_attr_in3_input.dev_attr.attr,
622 &sensor_dev_attr_in3_min.dev_attr.attr,
623 &sensor_dev_attr_in3_max.dev_attr.attr,
624 &sensor_dev_attr_in4_input.dev_attr.attr,
625 &sensor_dev_attr_in4_min.dev_attr.attr,
626 &sensor_dev_attr_in4_max.dev_attr.attr,
627 &sensor_dev_attr_in5_input.dev_attr.attr,
628 &sensor_dev_attr_in5_min.dev_attr.attr,
629 &sensor_dev_attr_in5_max.dev_attr.attr,
630 &sensor_dev_attr_in6_input.dev_attr.attr,
631 &sensor_dev_attr_in6_min.dev_attr.attr,
632 &sensor_dev_attr_in6_max.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200633
Jean Delvarefad33c52008-01-03 23:21:07 +0100634 &sensor_dev_attr_fan1_input.dev_attr.attr,
635 &sensor_dev_attr_fan1_min.dev_attr.attr,
636 &sensor_dev_attr_fan1_div.dev_attr.attr,
637 &sensor_dev_attr_fan2_input.dev_attr.attr,
638 &sensor_dev_attr_fan2_min.dev_attr.attr,
639 &sensor_dev_attr_fan2_div.dev_attr.attr,
640 &sensor_dev_attr_fan3_input.dev_attr.attr,
641 &sensor_dev_attr_fan3_min.dev_attr.attr,
642 &sensor_dev_attr_fan3_div.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200643
Jean Delvarefad33c52008-01-03 23:21:07 +0100644 &sensor_dev_attr_temp1_input.dev_attr.attr,
645 &sensor_dev_attr_temp1_max.dev_attr.attr,
646 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
647 &sensor_dev_attr_temp2_input.dev_attr.attr,
648 &sensor_dev_attr_temp2_max.dev_attr.attr,
649 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
650 &sensor_dev_attr_temp3_input.dev_attr.attr,
651 &sensor_dev_attr_temp3_max.dev_attr.attr,
652 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
653 &sensor_dev_attr_temp4_input.dev_attr.attr,
654 &sensor_dev_attr_temp4_max.dev_attr.attr,
655 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200656
Jean Delvare636866b2008-01-03 23:24:24 +0100657 &sensor_dev_attr_in0_alarm.dev_attr.attr,
658 &sensor_dev_attr_in1_alarm.dev_attr.attr,
659 &sensor_dev_attr_in2_alarm.dev_attr.attr,
660 &sensor_dev_attr_in3_alarm.dev_attr.attr,
661 &sensor_dev_attr_in4_alarm.dev_attr.attr,
662 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
663 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
664 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
665 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
666 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
667 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
668
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200669 &dev_attr_cpu0_vid.attr,
670 &dev_attr_vrm.attr,
671 &dev_attr_alarms.attr,
672 &dev_attr_pwm1.attr,
673 &dev_attr_pwm1_enable.attr,
674
675 NULL
676};
677
678static const struct attribute_group asb100_group = {
679 .attrs = asb100_attributes,
680};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jean Delvare063675b2008-07-16 19:30:11 +0200682static int asb100_detect_subclients(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 int i, id, err;
Jean Delvare063675b2008-07-16 19:30:11 +0200685 int address = client->addr;
686 unsigned short sc_addr[2];
Jean Delvareaf2219312008-01-03 23:15:49 +0100687 struct asb100_data *data = i2c_get_clientdata(client);
Jean Delvare063675b2008-07-16 19:30:11 +0200688 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 id = i2c_adapter_id(adapter);
691
692 if (force_subclients[0] == id && force_subclients[1] == address) {
693 for (i = 2; i <= 3; i++) {
694 if (force_subclients[i] < 0x48 ||
695 force_subclients[i] > 0x4f) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800696 dev_err(&client->dev,
697 "invalid subclient address %d; must be 0x48-0x4f\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 force_subclients[i]);
699 err = -ENODEV;
700 goto ERROR_SC_2;
701 }
702 }
Jean Delvareaf2219312008-01-03 23:15:49 +0100703 asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 (force_subclients[2] & 0x07) |
Jean Delvareaf2219312008-01-03 23:15:49 +0100705 ((force_subclients[3] & 0x07) << 4));
Jean Delvare063675b2008-07-16 19:30:11 +0200706 sc_addr[0] = force_subclients[2];
707 sc_addr[1] = force_subclients[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 } else {
Jean Delvareaf2219312008-01-03 23:15:49 +0100709 int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
Jean Delvare063675b2008-07-16 19:30:11 +0200710 sc_addr[0] = 0x48 + (val & 0x07);
711 sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Jean Delvare063675b2008-07-16 19:30:11 +0200714 if (sc_addr[0] == sc_addr[1]) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800715 dev_err(&client->dev,
716 "duplicate addresses 0x%x for subclients\n",
717 sc_addr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = -ENODEV;
719 goto ERROR_SC_2;
720 }
721
Jean Delvare063675b2008-07-16 19:30:11 +0200722 data->lm75[0] = i2c_new_dummy(adapter, sc_addr[0]);
723 if (!data->lm75[0]) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800724 dev_err(&client->dev,
725 "subclient %d registration at address 0x%x failed.\n",
726 1, sc_addr[0]);
Jean Delvare063675b2008-07-16 19:30:11 +0200727 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto ERROR_SC_2;
729 }
730
Jean Delvare063675b2008-07-16 19:30:11 +0200731 data->lm75[1] = i2c_new_dummy(adapter, sc_addr[1]);
732 if (!data->lm75[1]) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800733 dev_err(&client->dev,
734 "subclient %d registration at address 0x%x failed.\n",
735 2, sc_addr[1]);
Jean Delvare063675b2008-07-16 19:30:11 +0200736 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto ERROR_SC_3;
738 }
739
740 return 0;
741
742/* Undo inits in case of errors */
743ERROR_SC_3:
Jean Delvare063675b2008-07-16 19:30:11 +0200744 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745ERROR_SC_2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return err;
747}
748
Jean Delvare063675b2008-07-16 19:30:11 +0200749/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100750static int asb100_detect(struct i2c_client *client,
Jean Delvare063675b2008-07-16 19:30:11 +0200751 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Jean Delvare063675b2008-07-16 19:30:11 +0200753 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100754 int val1, val2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Joe Perches4d630e22010-10-20 06:51:29 +0000757 pr_debug("detect failed, smbus byte data not supported!\n");
Jean Delvare063675b2008-07-16 19:30:11 +0200758 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Jean Delvare52df6442009-12-09 20:35:57 +0100761 val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
762 val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jean Delvare52df6442009-12-09 20:35:57 +0100764 /* If we're in bank 0 */
765 if ((!(val1 & 0x07)) &&
766 /* Check for ASB100 ID (low byte) */
767 (((!(val1 & 0x80)) && (val2 != 0x94)) ||
768 /* Check for ASB100 ID (high byte ) */
769 ((val1 & 0x80) && (val2 != 0x06)))) {
Joe Perches4d630e22010-10-20 06:51:29 +0000770 pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
Jean Delvare52df6442009-12-09 20:35:57 +0100771 return -ENODEV;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jean Delvare52df6442009-12-09 20:35:57 +0100774 /* Put it now into bank 0 and Vendor ID High Byte */
Jean Delvare063675b2008-07-16 19:30:11 +0200775 i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
776 (i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
777 | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* Determine the chip type. */
Jean Delvare52df6442009-12-09 20:35:57 +0100780 val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
781 val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jean Delvare52df6442009-12-09 20:35:57 +0100783 if (val1 != 0x31 || val2 != 0x06)
784 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jean Delvare063675b2008-07-16 19:30:11 +0200786 strlcpy(info->type, "asb100", I2C_NAME_SIZE);
787
788 return 0;
789}
790
791static int asb100_probe(struct i2c_client *client,
792 const struct i2c_device_id *id)
793{
794 int err;
795 struct asb100_data *data;
796
Guenter Roeck64adf392012-06-02 09:58:02 -0700797 data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
798 GFP_KERNEL);
799 if (!data)
800 return -ENOMEM;
Jean Delvare063675b2008-07-16 19:30:11 +0200801
802 i2c_set_clientdata(client, data);
803 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100804 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* Attach secondary lm75 clients */
Jean Delvare063675b2008-07-16 19:30:11 +0200807 err = asb100_detect_subclients(client);
808 if (err)
Guenter Roeck64adf392012-06-02 09:58:02 -0700809 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Initialize the chip */
Jean Delvareaf2219312008-01-03 23:15:49 +0100812 asb100_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* A few vars need to be filled upon startup */
Jean Delvareaf2219312008-01-03 23:15:49 +0100815 data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
816 data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
817 data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* Register sysfs hooks */
Guenter Roeck8c103692012-01-14 13:20:00 -0800820 err = sysfs_create_group(&client->dev.kobj, &asb100_group);
821 if (err)
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200822 goto ERROR3;
823
Jean Delvareaf2219312008-01-03 23:15:49 +0100824 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700825 if (IS_ERR(data->hwmon_dev)) {
826 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200827 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200832ERROR4:
Jean Delvareaf2219312008-01-03 23:15:49 +0100833 sysfs_remove_group(&client->dev.kobj, &asb100_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400834ERROR3:
Jean Delvare063675b2008-07-16 19:30:11 +0200835 i2c_unregister_device(data->lm75[1]);
836 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return err;
838}
839
Jean Delvare063675b2008-07-16 19:30:11 +0200840static int asb100_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400842 struct asb100_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jean Delvare063675b2008-07-16 19:30:11 +0200844 hwmon_device_unregister(data->hwmon_dev);
845 sysfs_remove_group(&client->dev.kobj, &asb100_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400846
Jean Delvare063675b2008-07-16 19:30:11 +0200847 i2c_unregister_device(data->lm75[1]);
848 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851}
852
Guenter Roeck8c103692012-01-14 13:20:00 -0800853/*
854 * The SMBus locks itself, usually, but nothing may access the chip between
855 * bank switches.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static int asb100_read_value(struct i2c_client *client, u16 reg)
858{
859 struct asb100_data *data = i2c_get_clientdata(client);
860 struct i2c_client *cl;
861 int res, bank;
862
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100863 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 bank = (reg >> 8) & 0x0f;
866 if (bank > 2)
867 /* switch banks */
868 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
869
870 if (bank == 0 || bank > 2) {
871 res = i2c_smbus_read_byte_data(client, reg & 0xff);
872 } else {
873 /* switch to subclient */
874 cl = data->lm75[bank - 1];
875
876 /* convert from ISA to LM75 I2C addresses */
877 switch (reg & 0xff) {
878 case 0x50: /* TEMP */
Jean Delvare90f41022011-11-04 12:00:47 +0100879 res = i2c_smbus_read_word_swapped(cl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881 case 0x52: /* CONFIG */
882 res = i2c_smbus_read_byte_data(cl, 1);
883 break;
884 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +0100885 res = i2c_smbus_read_word_swapped(cl, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 case 0x55: /* MAX */
888 default:
Jean Delvare90f41022011-11-04 12:00:47 +0100889 res = i2c_smbus_read_word_swapped(cl, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
891 }
892 }
893
894 if (bank > 2)
895 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
896
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100897 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return res;
900}
901
902static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
903{
904 struct asb100_data *data = i2c_get_clientdata(client);
905 struct i2c_client *cl;
906 int bank;
907
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100908 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 bank = (reg >> 8) & 0x0f;
911 if (bank > 2)
912 /* switch banks */
913 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
914
915 if (bank == 0 || bank > 2) {
916 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
917 } else {
918 /* switch to subclient */
919 cl = data->lm75[bank - 1];
920
921 /* convert from ISA to LM75 I2C addresses */
922 switch (reg & 0xff) {
923 case 0x52: /* CONFIG */
924 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
925 break;
926 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +0100927 i2c_smbus_write_word_swapped(cl, 2, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 break;
929 case 0x55: /* MAX */
Jean Delvare90f41022011-11-04 12:00:47 +0100930 i2c_smbus_write_word_swapped(cl, 3, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
932 }
933 }
934
935 if (bank > 2)
936 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
937
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100938 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941static void asb100_init_client(struct i2c_client *client)
942{
943 struct asb100_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Jean Delvare303760b2005-07-31 21:52:01 +0200945 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* Start monitoring */
Jean Delvareaf2219312008-01-03 23:15:49 +0100948 asb100_write_value(client, ASB100_REG_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
950}
951
952static struct asb100_data *asb100_update_device(struct device *dev)
953{
954 struct i2c_client *client = to_i2c_client(dev);
955 struct asb100_data *data = i2c_get_clientdata(client);
956 int i;
957
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100958 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
961 || !data->valid) {
962
963 dev_dbg(&client->dev, "starting device update...\n");
964
965 /* 7 voltage inputs */
966 for (i = 0; i < 7; i++) {
967 data->in[i] = asb100_read_value(client,
968 ASB100_REG_IN(i));
969 data->in_min[i] = asb100_read_value(client,
970 ASB100_REG_IN_MIN(i));
971 data->in_max[i] = asb100_read_value(client,
972 ASB100_REG_IN_MAX(i));
973 }
974
975 /* 3 fan inputs */
976 for (i = 0; i < 3; i++) {
977 data->fan[i] = asb100_read_value(client,
978 ASB100_REG_FAN(i));
979 data->fan_min[i] = asb100_read_value(client,
980 ASB100_REG_FAN_MIN(i));
981 }
982
983 /* 4 temperature inputs */
984 for (i = 1; i <= 4; i++) {
985 data->temp[i-1] = asb100_read_value(client,
986 ASB100_REG_TEMP(i));
987 data->temp_max[i-1] = asb100_read_value(client,
988 ASB100_REG_TEMP_MAX(i));
989 data->temp_hyst[i-1] = asb100_read_value(client,
990 ASB100_REG_TEMP_HYST(i));
991 }
992
993 /* VID and fan divisors */
994 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
995 data->vid = i & 0x0f;
996 data->vid |= (asb100_read_value(client,
997 ASB100_REG_CHIPID) & 0x01) << 4;
998 data->fan_div[0] = (i >> 4) & 0x03;
999 data->fan_div[1] = (i >> 6) & 0x03;
1000 data->fan_div[2] = (asb100_read_value(client,
1001 ASB100_REG_PIN) >> 6) & 0x03;
1002
1003 /* PWM */
1004 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1005
1006 /* alarms */
1007 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1008 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1009
1010 data->last_updated = jiffies;
1011 data->valid = 1;
1012
1013 dev_dbg(&client->dev, "... device update complete\n");
1014 }
1015
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001016 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 return data;
1019}
1020
Axel Linf0967ee2012-01-20 15:38:18 +08001021module_i2c_driver(asb100_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1024MODULE_DESCRIPTION("ASB100 Bach driver");
1025MODULE_LICENSE("GPL");