1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Device driver for the i2c thermostat found on the iBook G4, Albook G4
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Documentation from 115254175ADT7467_pra.pdf and 3686221171167ADT7460_b.pdf
8*4882a593Smuzhiyun * https://www.onsemi.com/PowerSolutions/product.do?id=ADT7467
9*4882a593Smuzhiyun * https://www.onsemi.com/PowerSolutions/product.do?id=ADT7460
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/sched.h>
19*4882a593Smuzhiyun #include <linux/i2c.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/spinlock.h>
23*4882a593Smuzhiyun #include <linux/wait.h>
24*4882a593Smuzhiyun #include <linux/suspend.h>
25*4882a593Smuzhiyun #include <linux/kthread.h>
26*4882a593Smuzhiyun #include <linux/moduleparam.h>
27*4882a593Smuzhiyun #include <linux/freezer.h>
28*4882a593Smuzhiyun #include <linux/of_platform.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <asm/prom.h>
31*4882a593Smuzhiyun #include <asm/machdep.h>
32*4882a593Smuzhiyun #include <asm/io.h>
33*4882a593Smuzhiyun #include <asm/sections.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #undef DEBUG
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define CONFIG_REG 0x40
38*4882a593Smuzhiyun #define MANUAL_MASK 0xe0
39*4882a593Smuzhiyun #define AUTO_MASK 0x20
40*4882a593Smuzhiyun #define INVERT_MASK 0x10
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
43*4882a593Smuzhiyun static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
44*4882a593Smuzhiyun static u8 MANUAL_MODE[2] = {0x5c, 0x5d};
45*4882a593Smuzhiyun static u8 REM_CONTROL[2] = {0x00, 0x40};
46*4882a593Smuzhiyun static u8 FAN_SPEED[2] = {0x28, 0x2a};
47*4882a593Smuzhiyun static u8 FAN_SPD_SET[2] = {0x30, 0x31};
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */
50*4882a593Smuzhiyun static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */
51*4882a593Smuzhiyun static const char *sensor_location[3] = { "?", "?", "?" };
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun static int limit_adjust;
54*4882a593Smuzhiyun static int fan_speed = -1;
55*4882a593Smuzhiyun static bool verbose;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
58*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
59*4882a593Smuzhiyun "Powerbook G4 Alu");
60*4882a593Smuzhiyun MODULE_LICENSE("GPL");
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun module_param(limit_adjust, int, 0644);
63*4882a593Smuzhiyun MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
64*4882a593Smuzhiyun "by N degrees.");
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun module_param(fan_speed, int, 0644);
67*4882a593Smuzhiyun MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
68*4882a593Smuzhiyun "(default 64)");
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun module_param(verbose, bool, 0);
71*4882a593Smuzhiyun MODULE_PARM_DESC(verbose,"Verbose log operations "
72*4882a593Smuzhiyun "(default 0)");
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun struct thermostat {
75*4882a593Smuzhiyun struct i2c_client *clt;
76*4882a593Smuzhiyun u8 temps[3];
77*4882a593Smuzhiyun u8 cached_temp[3];
78*4882a593Smuzhiyun u8 initial_limits[3];
79*4882a593Smuzhiyun u8 limits[3];
80*4882a593Smuzhiyun int last_speed[2];
81*4882a593Smuzhiyun int last_var[2];
82*4882a593Smuzhiyun int pwm_inv[2];
83*4882a593Smuzhiyun struct task_struct *thread;
84*4882a593Smuzhiyun struct platform_device *pdev;
85*4882a593Smuzhiyun enum {
86*4882a593Smuzhiyun ADT7460,
87*4882a593Smuzhiyun ADT7467
88*4882a593Smuzhiyun } type;
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static void write_both_fan_speed(struct thermostat *th, int speed);
92*4882a593Smuzhiyun static void write_fan_speed(struct thermostat *th, int speed, int fan);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun static int
write_reg(struct thermostat * th,int reg,u8 data)95*4882a593Smuzhiyun write_reg(struct thermostat* th, int reg, u8 data)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun u8 tmp[2];
98*4882a593Smuzhiyun int rc;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun tmp[0] = reg;
101*4882a593Smuzhiyun tmp[1] = data;
102*4882a593Smuzhiyun rc = i2c_master_send(th->clt, (const char *)tmp, 2);
103*4882a593Smuzhiyun if (rc < 0)
104*4882a593Smuzhiyun return rc;
105*4882a593Smuzhiyun if (rc != 2)
106*4882a593Smuzhiyun return -ENODEV;
107*4882a593Smuzhiyun return 0;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun static int
read_reg(struct thermostat * th,int reg)111*4882a593Smuzhiyun read_reg(struct thermostat* th, int reg)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun u8 reg_addr, data;
114*4882a593Smuzhiyun int rc;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun reg_addr = (u8)reg;
117*4882a593Smuzhiyun rc = i2c_master_send(th->clt, ®_addr, 1);
118*4882a593Smuzhiyun if (rc < 0)
119*4882a593Smuzhiyun return rc;
120*4882a593Smuzhiyun if (rc != 1)
121*4882a593Smuzhiyun return -ENODEV;
122*4882a593Smuzhiyun rc = i2c_master_recv(th->clt, (char *)&data, 1);
123*4882a593Smuzhiyun if (rc < 0)
124*4882a593Smuzhiyun return rc;
125*4882a593Smuzhiyun return data;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
read_fan_speed(struct thermostat * th,u8 addr)128*4882a593Smuzhiyun static int read_fan_speed(struct thermostat *th, u8 addr)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun u8 tmp[2];
131*4882a593Smuzhiyun u16 res;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* should start with low byte */
134*4882a593Smuzhiyun tmp[1] = read_reg(th, addr);
135*4882a593Smuzhiyun tmp[0] = read_reg(th, addr + 1);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun res = tmp[1] + (tmp[0] << 8);
138*4882a593Smuzhiyun /* "a value of 0xffff means that the fan has stopped" */
139*4882a593Smuzhiyun return (res == 0xffff ? 0 : (90000*60)/res);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
write_both_fan_speed(struct thermostat * th,int speed)142*4882a593Smuzhiyun static void write_both_fan_speed(struct thermostat *th, int speed)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun write_fan_speed(th, speed, 0);
145*4882a593Smuzhiyun if (th->type == ADT7460)
146*4882a593Smuzhiyun write_fan_speed(th, speed, 1);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
write_fan_speed(struct thermostat * th,int speed,int fan)149*4882a593Smuzhiyun static void write_fan_speed(struct thermostat *th, int speed, int fan)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun u8 manual;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (speed > 0xff)
154*4882a593Smuzhiyun speed = 0xff;
155*4882a593Smuzhiyun else if (speed < -1)
156*4882a593Smuzhiyun speed = 0;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun if (th->type == ADT7467 && fan == 1)
159*4882a593Smuzhiyun return;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (th->last_speed[fan] != speed) {
162*4882a593Smuzhiyun if (verbose) {
163*4882a593Smuzhiyun if (speed == -1)
164*4882a593Smuzhiyun printk(KERN_DEBUG "adt746x: Setting speed to automatic "
165*4882a593Smuzhiyun "for %s fan.\n", sensor_location[fan+1]);
166*4882a593Smuzhiyun else
167*4882a593Smuzhiyun printk(KERN_DEBUG "adt746x: Setting speed to %d "
168*4882a593Smuzhiyun "for %s fan.\n", speed, sensor_location[fan+1]);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun } else
171*4882a593Smuzhiyun return;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (speed >= 0) {
174*4882a593Smuzhiyun manual = read_reg(th, MANUAL_MODE[fan]);
175*4882a593Smuzhiyun manual &= ~INVERT_MASK;
176*4882a593Smuzhiyun write_reg(th, MANUAL_MODE[fan],
177*4882a593Smuzhiyun manual | MANUAL_MASK | th->pwm_inv[fan]);
178*4882a593Smuzhiyun write_reg(th, FAN_SPD_SET[fan], speed);
179*4882a593Smuzhiyun } else {
180*4882a593Smuzhiyun /* back to automatic */
181*4882a593Smuzhiyun if(th->type == ADT7460) {
182*4882a593Smuzhiyun manual = read_reg(th,
183*4882a593Smuzhiyun MANUAL_MODE[fan]) & (~MANUAL_MASK);
184*4882a593Smuzhiyun manual &= ~INVERT_MASK;
185*4882a593Smuzhiyun manual |= th->pwm_inv[fan];
186*4882a593Smuzhiyun write_reg(th,
187*4882a593Smuzhiyun MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
188*4882a593Smuzhiyun } else {
189*4882a593Smuzhiyun manual = read_reg(th, MANUAL_MODE[fan]);
190*4882a593Smuzhiyun manual &= ~INVERT_MASK;
191*4882a593Smuzhiyun manual |= th->pwm_inv[fan];
192*4882a593Smuzhiyun write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun th->last_speed[fan] = speed;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
read_sensors(struct thermostat * th)199*4882a593Smuzhiyun static void read_sensors(struct thermostat *th)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun int i = 0;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun for (i = 0; i < 3; i++)
204*4882a593Smuzhiyun th->temps[i] = read_reg(th, TEMP_REG[i]);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun #ifdef DEBUG
display_stats(struct thermostat * th)208*4882a593Smuzhiyun static void display_stats(struct thermostat *th)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun if (th->temps[0] != th->cached_temp[0]
211*4882a593Smuzhiyun || th->temps[1] != th->cached_temp[1]
212*4882a593Smuzhiyun || th->temps[2] != th->cached_temp[2]) {
213*4882a593Smuzhiyun printk(KERN_INFO "adt746x: Temperature infos:"
214*4882a593Smuzhiyun " thermostats: %d,%d,%d;"
215*4882a593Smuzhiyun " limits: %d,%d,%d;"
216*4882a593Smuzhiyun " fan speed: %d RPM\n",
217*4882a593Smuzhiyun th->temps[0], th->temps[1], th->temps[2],
218*4882a593Smuzhiyun th->limits[0], th->limits[1], th->limits[2],
219*4882a593Smuzhiyun read_fan_speed(th, FAN_SPEED[0]));
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun th->cached_temp[0] = th->temps[0];
222*4882a593Smuzhiyun th->cached_temp[1] = th->temps[1];
223*4882a593Smuzhiyun th->cached_temp[2] = th->temps[2];
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun #endif
226*4882a593Smuzhiyun
update_fans_speed(struct thermostat * th)227*4882a593Smuzhiyun static void update_fans_speed (struct thermostat *th)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun int lastvar = 0; /* last variation, for iBook */
230*4882a593Smuzhiyun int i = 0;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* we don't care about local sensor, so we start at sensor 1 */
233*4882a593Smuzhiyun for (i = 1; i < 3; i++) {
234*4882a593Smuzhiyun bool started = false;
235*4882a593Smuzhiyun int fan_number = (th->type == ADT7460 && i == 2);
236*4882a593Smuzhiyun int var = th->temps[i] - th->limits[i];
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (var > -1) {
239*4882a593Smuzhiyun int step = (255 - fan_speed) / 7;
240*4882a593Smuzhiyun int new_speed = 0;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* hysteresis : change fan speed only if variation is
243*4882a593Smuzhiyun * more than two degrees */
244*4882a593Smuzhiyun if (abs(var - th->last_var[fan_number]) < 2)
245*4882a593Smuzhiyun continue;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun started = true;
248*4882a593Smuzhiyun new_speed = fan_speed + ((var-1)*step);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun if (new_speed < fan_speed)
251*4882a593Smuzhiyun new_speed = fan_speed;
252*4882a593Smuzhiyun if (new_speed > 255)
253*4882a593Smuzhiyun new_speed = 255;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun if (verbose)
256*4882a593Smuzhiyun printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
257*4882a593Smuzhiyun "(limit exceeded by %d on %s)\n",
258*4882a593Smuzhiyun new_speed, var,
259*4882a593Smuzhiyun sensor_location[fan_number+1]);
260*4882a593Smuzhiyun write_both_fan_speed(th, new_speed);
261*4882a593Smuzhiyun th->last_var[fan_number] = var;
262*4882a593Smuzhiyun } else if (var < -2) {
263*4882a593Smuzhiyun /* don't stop fan if sensor2 is cold and sensor1 is not
264*4882a593Smuzhiyun * so cold (lastvar >= -1) */
265*4882a593Smuzhiyun if (i == 2 && lastvar < -1) {
266*4882a593Smuzhiyun if (th->last_speed[fan_number] != 0)
267*4882a593Smuzhiyun if (verbose)
268*4882a593Smuzhiyun printk(KERN_DEBUG "adt746x: Stopping "
269*4882a593Smuzhiyun "fans.\n");
270*4882a593Smuzhiyun write_both_fan_speed(th, 0);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun lastvar = var;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (started)
277*4882a593Smuzhiyun return; /* we don't want to re-stop the fan
278*4882a593Smuzhiyun * if sensor1 is heating and sensor2 is not */
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
monitor_task(void * arg)282*4882a593Smuzhiyun static int monitor_task(void *arg)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun struct thermostat* th = arg;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun set_freezable();
287*4882a593Smuzhiyun while(!kthread_should_stop()) {
288*4882a593Smuzhiyun try_to_freeze();
289*4882a593Smuzhiyun msleep_interruptible(2000);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun #ifndef DEBUG
292*4882a593Smuzhiyun if (fan_speed != -1)
293*4882a593Smuzhiyun read_sensors(th);
294*4882a593Smuzhiyun #else
295*4882a593Smuzhiyun read_sensors(th);
296*4882a593Smuzhiyun #endif
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (fan_speed != -1)
299*4882a593Smuzhiyun update_fans_speed(th);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun #ifdef DEBUG
302*4882a593Smuzhiyun display_stats(th);
303*4882a593Smuzhiyun #endif
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun return 0;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
set_limit(struct thermostat * th,int i)310*4882a593Smuzhiyun static void set_limit(struct thermostat *th, int i)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun /* Set sensor1 limit higher to avoid powerdowns */
313*4882a593Smuzhiyun th->limits[i] = default_limits_chip[i] + limit_adjust;
314*4882a593Smuzhiyun write_reg(th, LIMIT_REG[i], th->limits[i]);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /* set our limits to normal */
317*4882a593Smuzhiyun th->limits[i] = default_limits_local[i] + limit_adjust;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun #define BUILD_SHOW_FUNC_INT(name, data) \
321*4882a593Smuzhiyun static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
322*4882a593Smuzhiyun { \
323*4882a593Smuzhiyun struct thermostat *th = dev_get_drvdata(dev); \
324*4882a593Smuzhiyun return sprintf(buf, "%d\n", data); \
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun #define BUILD_SHOW_FUNC_INT_LITE(name, data) \
328*4882a593Smuzhiyun static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
329*4882a593Smuzhiyun { \
330*4882a593Smuzhiyun return sprintf(buf, "%d\n", data); \
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun #define BUILD_SHOW_FUNC_STR(name, data) \
334*4882a593Smuzhiyun static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
335*4882a593Smuzhiyun { \
336*4882a593Smuzhiyun return sprintf(buf, "%s\n", data); \
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun #define BUILD_SHOW_FUNC_FAN(name, data) \
340*4882a593Smuzhiyun static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
341*4882a593Smuzhiyun { \
342*4882a593Smuzhiyun struct thermostat *th = dev_get_drvdata(dev); \
343*4882a593Smuzhiyun return sprintf(buf, "%d (%d rpm)\n", \
344*4882a593Smuzhiyun th->last_speed[data], \
345*4882a593Smuzhiyun read_fan_speed(th, FAN_SPEED[data]) \
346*4882a593Smuzhiyun ); \
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun #define BUILD_STORE_FUNC_DEG(name, data) \
350*4882a593Smuzhiyun static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
351*4882a593Smuzhiyun { \
352*4882a593Smuzhiyun struct thermostat *th = dev_get_drvdata(dev); \
353*4882a593Smuzhiyun int val; \
354*4882a593Smuzhiyun int i; \
355*4882a593Smuzhiyun val = simple_strtol(buf, NULL, 10); \
356*4882a593Smuzhiyun printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \
357*4882a593Smuzhiyun limit_adjust = val; \
358*4882a593Smuzhiyun for (i=0; i < 3; i++) \
359*4882a593Smuzhiyun set_limit(th, i); \
360*4882a593Smuzhiyun return n; \
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun #define BUILD_STORE_FUNC_INT(name, data) \
364*4882a593Smuzhiyun static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
365*4882a593Smuzhiyun { \
366*4882a593Smuzhiyun int val; \
367*4882a593Smuzhiyun val = simple_strtol(buf, NULL, 10); \
368*4882a593Smuzhiyun if (val < 0 || val > 255) \
369*4882a593Smuzhiyun return -EINVAL; \
370*4882a593Smuzhiyun printk(KERN_INFO "Setting specified fan speed to %d\n", val); \
371*4882a593Smuzhiyun data = val; \
372*4882a593Smuzhiyun return n; \
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(th, TEMP_REG[1])))
376*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(th, TEMP_REG[2])))
377*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT(sensor1_limit, th->limits[1])
378*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT(sensor2_limit, th->limits[2])
379*4882a593Smuzhiyun BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1])
380*4882a593Smuzhiyun BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2])
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT_LITE(specified_fan_speed, fan_speed)
383*4882a593Smuzhiyun BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0)
386*4882a593Smuzhiyun BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1)
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun BUILD_SHOW_FUNC_INT_LITE(limit_adjust, limit_adjust)
389*4882a593Smuzhiyun BUILD_STORE_FUNC_DEG(limit_adjust, th)
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun static DEVICE_ATTR(sensor1_temperature, S_IRUGO,
392*4882a593Smuzhiyun show_sensor1_temperature,NULL);
393*4882a593Smuzhiyun static DEVICE_ATTR(sensor2_temperature, S_IRUGO,
394*4882a593Smuzhiyun show_sensor2_temperature,NULL);
395*4882a593Smuzhiyun static DEVICE_ATTR(sensor1_limit, S_IRUGO,
396*4882a593Smuzhiyun show_sensor1_limit, NULL);
397*4882a593Smuzhiyun static DEVICE_ATTR(sensor2_limit, S_IRUGO,
398*4882a593Smuzhiyun show_sensor2_limit, NULL);
399*4882a593Smuzhiyun static DEVICE_ATTR(sensor1_location, S_IRUGO,
400*4882a593Smuzhiyun show_sensor1_location, NULL);
401*4882a593Smuzhiyun static DEVICE_ATTR(sensor2_location, S_IRUGO,
402*4882a593Smuzhiyun show_sensor2_location, NULL);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
405*4882a593Smuzhiyun show_specified_fan_speed,store_specified_fan_speed);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO,
408*4882a593Smuzhiyun show_sensor1_fan_speed, NULL);
409*4882a593Smuzhiyun static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO,
410*4882a593Smuzhiyun show_sensor2_fan_speed, NULL);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
413*4882a593Smuzhiyun show_limit_adjust, store_limit_adjust);
414*4882a593Smuzhiyun
thermostat_create_files(struct thermostat * th)415*4882a593Smuzhiyun static void thermostat_create_files(struct thermostat *th)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun struct device_node *np = th->clt->dev.of_node;
418*4882a593Smuzhiyun struct device *dev;
419*4882a593Smuzhiyun int err;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* To maintain ABI compatibility with userspace, create
422*4882a593Smuzhiyun * the old style platform driver and attach the attributes
423*4882a593Smuzhiyun * to it here
424*4882a593Smuzhiyun */
425*4882a593Smuzhiyun th->pdev = of_platform_device_create(np, "temperatures", NULL);
426*4882a593Smuzhiyun if (!th->pdev)
427*4882a593Smuzhiyun return;
428*4882a593Smuzhiyun dev = &th->pdev->dev;
429*4882a593Smuzhiyun dev_set_drvdata(dev, th);
430*4882a593Smuzhiyun err = device_create_file(dev, &dev_attr_sensor1_temperature);
431*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor2_temperature);
432*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor1_limit);
433*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor2_limit);
434*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor1_location);
435*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor2_location);
436*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_limit_adjust);
437*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_specified_fan_speed);
438*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor1_fan_speed);
439*4882a593Smuzhiyun if(th->type == ADT7460)
440*4882a593Smuzhiyun err |= device_create_file(dev, &dev_attr_sensor2_fan_speed);
441*4882a593Smuzhiyun if (err)
442*4882a593Smuzhiyun printk(KERN_WARNING
443*4882a593Smuzhiyun "Failed to create temperature attribute file(s).\n");
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
thermostat_remove_files(struct thermostat * th)446*4882a593Smuzhiyun static void thermostat_remove_files(struct thermostat *th)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun struct device *dev;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (!th->pdev)
451*4882a593Smuzhiyun return;
452*4882a593Smuzhiyun dev = &th->pdev->dev;
453*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor1_temperature);
454*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor2_temperature);
455*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor1_limit);
456*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor2_limit);
457*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor1_location);
458*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor2_location);
459*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_limit_adjust);
460*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_specified_fan_speed);
461*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor1_fan_speed);
462*4882a593Smuzhiyun if (th->type == ADT7460)
463*4882a593Smuzhiyun device_remove_file(dev, &dev_attr_sensor2_fan_speed);
464*4882a593Smuzhiyun of_device_unregister(th->pdev);
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
probe_thermostat(struct i2c_client * client,const struct i2c_device_id * id)468*4882a593Smuzhiyun static int probe_thermostat(struct i2c_client *client,
469*4882a593Smuzhiyun const struct i2c_device_id *id)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun struct device_node *np = client->dev.of_node;
472*4882a593Smuzhiyun struct thermostat* th;
473*4882a593Smuzhiyun const __be32 *prop;
474*4882a593Smuzhiyun int i, rc, vers, offset = 0;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun if (!np)
477*4882a593Smuzhiyun return -ENXIO;
478*4882a593Smuzhiyun prop = of_get_property(np, "hwsensor-params-version", NULL);
479*4882a593Smuzhiyun if (!prop)
480*4882a593Smuzhiyun return -ENXIO;
481*4882a593Smuzhiyun vers = be32_to_cpup(prop);
482*4882a593Smuzhiyun printk(KERN_INFO "adt746x: version %d (%ssupported)\n",
483*4882a593Smuzhiyun vers, vers == 1 ? "" : "un");
484*4882a593Smuzhiyun if (vers != 1)
485*4882a593Smuzhiyun return -ENXIO;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun if (of_get_property(np, "hwsensor-location", NULL)) {
488*4882a593Smuzhiyun for (i = 0; i < 3; i++) {
489*4882a593Smuzhiyun sensor_location[i] = of_get_property(np,
490*4882a593Smuzhiyun "hwsensor-location", NULL) + offset;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun if (sensor_location[i] == NULL)
493*4882a593Smuzhiyun sensor_location[i] = "";
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
496*4882a593Smuzhiyun offset += strlen(sensor_location[i]) + 1;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
501*4882a593Smuzhiyun if (!th)
502*4882a593Smuzhiyun return -ENOMEM;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun i2c_set_clientdata(client, th);
505*4882a593Smuzhiyun th->clt = client;
506*4882a593Smuzhiyun th->type = id->driver_data;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun rc = read_reg(th, CONFIG_REG);
509*4882a593Smuzhiyun if (rc < 0) {
510*4882a593Smuzhiyun dev_err(&client->dev, "Thermostat failed to read config!\n");
511*4882a593Smuzhiyun kfree(th);
512*4882a593Smuzhiyun return -ENODEV;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* force manual control to start the fan quieter */
516*4882a593Smuzhiyun if (fan_speed == -1)
517*4882a593Smuzhiyun fan_speed = 64;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (th->type == ADT7460) {
520*4882a593Smuzhiyun printk(KERN_INFO "adt746x: ADT7460 initializing\n");
521*4882a593Smuzhiyun /* The 7460 needs to be started explicitly */
522*4882a593Smuzhiyun write_reg(th, CONFIG_REG, 1);
523*4882a593Smuzhiyun } else
524*4882a593Smuzhiyun printk(KERN_INFO "adt746x: ADT7467 initializing\n");
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun for (i = 0; i < 3; i++) {
527*4882a593Smuzhiyun th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
528*4882a593Smuzhiyun set_limit(th, i);
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
532*4882a593Smuzhiyun " to %d, %d, %d\n",
533*4882a593Smuzhiyun th->initial_limits[0], th->initial_limits[1],
534*4882a593Smuzhiyun th->initial_limits[2], th->limits[0], th->limits[1],
535*4882a593Smuzhiyun th->limits[2]);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /* record invert bit status because fw can corrupt it after suspend */
538*4882a593Smuzhiyun th->pwm_inv[0] = read_reg(th, MANUAL_MODE[0]) & INVERT_MASK;
539*4882a593Smuzhiyun th->pwm_inv[1] = read_reg(th, MANUAL_MODE[1]) & INVERT_MASK;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun /* be sure to really write fan speed the first time */
542*4882a593Smuzhiyun th->last_speed[0] = -2;
543*4882a593Smuzhiyun th->last_speed[1] = -2;
544*4882a593Smuzhiyun th->last_var[0] = -80;
545*4882a593Smuzhiyun th->last_var[1] = -80;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun if (fan_speed != -1) {
548*4882a593Smuzhiyun /* manual mode, stop fans */
549*4882a593Smuzhiyun write_both_fan_speed(th, 0);
550*4882a593Smuzhiyun } else {
551*4882a593Smuzhiyun /* automatic mode */
552*4882a593Smuzhiyun write_both_fan_speed(th, -1);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun th->thread = kthread_run(monitor_task, th, "kfand");
556*4882a593Smuzhiyun if (th->thread == ERR_PTR(-ENOMEM)) {
557*4882a593Smuzhiyun printk(KERN_INFO "adt746x: Kthread creation failed\n");
558*4882a593Smuzhiyun th->thread = NULL;
559*4882a593Smuzhiyun return -ENOMEM;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun thermostat_create_files(th);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun return 0;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
remove_thermostat(struct i2c_client * client)567*4882a593Smuzhiyun static int remove_thermostat(struct i2c_client *client)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun struct thermostat *th = i2c_get_clientdata(client);
570*4882a593Smuzhiyun int i;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun thermostat_remove_files(th);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun if (th->thread != NULL)
575*4882a593Smuzhiyun kthread_stop(th->thread);
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun printk(KERN_INFO "adt746x: Putting max temperatures back from "
578*4882a593Smuzhiyun "%d, %d, %d to %d, %d, %d\n",
579*4882a593Smuzhiyun th->limits[0], th->limits[1], th->limits[2],
580*4882a593Smuzhiyun th->initial_limits[0], th->initial_limits[1],
581*4882a593Smuzhiyun th->initial_limits[2]);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun for (i = 0; i < 3; i++)
584*4882a593Smuzhiyun write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun write_both_fan_speed(th, -1);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun kfree(th);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun return 0;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun static const struct i2c_device_id therm_adt746x_id[] = {
594*4882a593Smuzhiyun { "MAC,adt7460", ADT7460 },
595*4882a593Smuzhiyun { "MAC,adt7467", ADT7467 },
596*4882a593Smuzhiyun { }
597*4882a593Smuzhiyun };
598*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, therm_adt746x_id);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun static struct i2c_driver thermostat_driver = {
601*4882a593Smuzhiyun .driver = {
602*4882a593Smuzhiyun .name = "therm_adt746x",
603*4882a593Smuzhiyun },
604*4882a593Smuzhiyun .probe = probe_thermostat,
605*4882a593Smuzhiyun .remove = remove_thermostat,
606*4882a593Smuzhiyun .id_table = therm_adt746x_id,
607*4882a593Smuzhiyun };
608*4882a593Smuzhiyun
thermostat_init(void)609*4882a593Smuzhiyun static int __init thermostat_init(void)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun #ifndef CONFIG_I2C_POWERMAC
612*4882a593Smuzhiyun request_module("i2c-powermac");
613*4882a593Smuzhiyun #endif
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun return i2c_add_driver(&thermostat_driver);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
thermostat_exit(void)618*4882a593Smuzhiyun static void __exit thermostat_exit(void)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun i2c_del_driver(&thermostat_driver);
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun module_init(thermostat_init);
624*4882a593Smuzhiyun module_exit(thermostat_exit);
625