2 * Windfarm PowerMac thermal control. AD7417 sensors
4 * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
6 * Released under the term of the GNU GPL v2.
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/i2c.h>
18 #include <asm/machdep.h>
20 #include <asm/sections.h>
23 #include "windfarm_mpu.h"
27 struct wf_ad7417_priv
{
29 struct i2c_client
*i2c
;
32 const struct mpu_data
*mpu
;
33 struct wf_sensor sensors
[5];
37 static int wf_ad7417_temp_get(struct wf_sensor
*sr
, s32
*value
)
39 struct wf_ad7417_priv
*pv
= sr
->priv
;
45 mutex_lock(&pv
->lock
);
47 /* Read temp register */
49 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
52 rc
= i2c_master_recv(pv
->i2c
, buf
, 2);
56 /* Read a a 16-bit signed value */
57 raw
= be16_to_cpup((__le16
*)buf
);
59 /* Convert 8.8-bit to 16.16 fixed point */
60 *value
= ((s32
)raw
) << 8;
62 mutex_unlock(&pv
->lock
);
66 mutex_unlock(&pv
->lock
);
71 * Scaling factors for the AD7417 ADC converters (except
72 * for the CPU diode which is obtained from the EEPROM).
73 * Those values are obtained from the property list of
76 #define ADC_12V_CURRENT_SCALE 0x0320 /* _AD2 */
77 #define ADC_CPU_VOLTAGE_SCALE 0x00a0 /* _AD3 */
78 #define ADC_CPU_CURRENT_SCALE 0x1f40 /* _AD4 */
80 static void wf_ad7417_adc_convert(struct wf_ad7417_priv
*pv
,
81 int chan
, s32 raw
, s32
*value
)
85 *value
= (raw
* (s32
)pv
->mpu
->mdiode
+
86 ((s32
)pv
->mpu
->bdiode
<< 12)) >> 2;
88 case 2: /* 12v current */
89 *value
= raw
* ADC_12V_CURRENT_SCALE
;
91 case 3: /* core voltage */
92 *value
= raw
* ADC_CPU_VOLTAGE_SCALE
;
94 case 4: /* core current */
95 *value
= raw
* ADC_CPU_CURRENT_SCALE
;
100 static int wf_ad7417_adc_get(struct wf_sensor
*sr
, s32
*value
)
102 struct wf_ad7417_priv
*pv
= sr
->priv
;
103 int chan
= sr
- pv
->sensors
;
109 mutex_lock(&pv
->lock
);
110 for (i
= 0; i
< 10; i
++) {
113 buf
[1] = (pv
->config
& 0x1f) | (chan
<< 5);
114 rc
= i2c_master_send(pv
->i2c
, buf
, 2);
118 /* Wait for conversion */
121 /* Switch to data register */
123 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
128 rc
= i2c_master_recv(pv
->i2c
, buf
, 2);
132 /* Read a a 16-bit signed value */
133 raw
= be16_to_cpup((__le16
*)buf
) >> 6;
134 wf_ad7417_adc_convert(pv
, chan
, raw
, value
);
136 dev_vdbg(&pv
->i2c
->dev
, "ADC chan %d [%s]"
137 " raw value: 0x%x, conv to: 0x%08x\n",
138 chan
, sr
->name
, raw
, *value
);
140 mutex_unlock(&pv
->lock
);
144 dev_dbg(&pv
->i2c
->dev
,
145 "Error reading ADC, try %d...\n", i
);
149 mutex_unlock(&pv
->lock
);
153 static void wf_ad7417_release(struct kref
*ref
)
155 struct wf_ad7417_priv
*pv
= container_of(ref
,
156 struct wf_ad7417_priv
, ref
);
160 static void wf_ad7417_sensor_release(struct wf_sensor
*sr
)
162 struct wf_ad7417_priv
*pv
= sr
->priv
;
165 kref_put(&pv
->ref
, wf_ad7417_release
);
168 static const struct wf_sensor_ops wf_ad7417_temp_ops
= {
169 .get_value
= wf_ad7417_temp_get
,
170 .release
= wf_ad7417_sensor_release
,
171 .owner
= THIS_MODULE
,
174 static const struct wf_sensor_ops wf_ad7417_adc_ops
= {
175 .get_value
= wf_ad7417_adc_get
,
176 .release
= wf_ad7417_sensor_release
,
177 .owner
= THIS_MODULE
,
180 static void wf_ad7417_add_sensor(struct wf_ad7417_priv
*pv
,
181 int index
, const char *name
,
182 const struct wf_sensor_ops
*ops
)
184 pv
->sensors
[index
].name
= kasprintf(GFP_KERNEL
, "%s-%d", name
, pv
->cpu
);
185 pv
->sensors
[index
].priv
= pv
;
186 pv
->sensors
[index
].ops
= ops
;
187 if (!wf_register_sensor(&pv
->sensors
[index
]))
191 static void wf_ad7417_init_chip(struct wf_ad7417_priv
*pv
)
198 * Read ADC the configuration register and cache it. We
199 * also make sure Config2 contains proper values, I've seen
200 * cases where we got stale grabage in there, thus preventing
201 * proper reading of conv. values
207 i2c_master_send(pv
->i2c
, buf
, 2);
209 /* Read & cache Config1 */
211 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
213 rc
= i2c_master_recv(pv
->i2c
, buf
, 1);
217 dev_dbg(&pv
->i2c
->dev
, "ADC config reg: %02x\n",
220 /* Disable shutdown mode */
224 rc
= i2c_master_send(pv
->i2c
, buf
, 2);
228 dev_err(&pv
->i2c
->dev
, "Error reading ADC config\n");
233 static int wf_ad7417_probe(struct i2c_client
*client
,
234 const struct i2c_device_id
*id
)
236 struct wf_ad7417_priv
*pv
;
237 const struct mpu_data
*mpu
;
241 loc
= of_get_property(client
->dev
.of_node
, "hwsensor-location", NULL
);
243 dev_warn(&client
->dev
, "Missing hwsensor-location property!\n");
248 * Identify which CPU we belong to by looking at the first entry
249 * in the hwsensor-location list
251 if (!strncmp(loc
, "CPU A", 5))
253 else if (!strncmp(loc
, "CPU B", 5))
256 pr_err("wf_ad7417: Can't identify location %s\n", loc
);
259 mpu
= wf_get_mpu(cpu_nr
);
261 dev_err(&client
->dev
, "Failed to retrieve MPU data\n");
265 pv
= kzalloc(sizeof(struct wf_ad7417_priv
), GFP_KERNEL
);
270 mutex_init(&pv
->lock
);
274 dev_set_drvdata(&client
->dev
, pv
);
276 /* Initialize the chip */
277 wf_ad7417_init_chip(pv
);
280 * We cannot rely on Apple device-tree giving us child
281 * node with the names of the individual sensors so we
282 * just hard code what we know about them
284 wf_ad7417_add_sensor(pv
, 0, "cpu-amb-temp", &wf_ad7417_temp_ops
);
285 wf_ad7417_add_sensor(pv
, 1, "cpu-diode-temp", &wf_ad7417_adc_ops
);
286 wf_ad7417_add_sensor(pv
, 2, "cpu-12v-current", &wf_ad7417_adc_ops
);
287 wf_ad7417_add_sensor(pv
, 3, "cpu-voltage", &wf_ad7417_adc_ops
);
288 wf_ad7417_add_sensor(pv
, 4, "cpu-current", &wf_ad7417_adc_ops
);
293 static int wf_ad7417_remove(struct i2c_client
*client
)
295 struct wf_ad7417_priv
*pv
= dev_get_drvdata(&client
->dev
);
298 /* Mark client detached */
302 for (i
= 0; i
< 5; i
++)
303 wf_unregister_sensor(&pv
->sensors
[i
]);
305 kref_put(&pv
->ref
, wf_ad7417_release
);
310 static const struct i2c_device_id wf_ad7417_id
[] = {
314 MODULE_DEVICE_TABLE(i2c
, wf_ad7417_id
);
316 static const struct of_device_id wf_ad7417_of_id
[] = {
317 { .compatible
= "ad7417", },
320 MODULE_DEVICE_TABLE(of
, wf_ad7417_of_id
);
322 static struct i2c_driver wf_ad7417_driver
= {
325 .of_match_table
= wf_ad7417_of_id
,
327 .probe
= wf_ad7417_probe
,
328 .remove
= wf_ad7417_remove
,
329 .id_table
= wf_ad7417_id
,
332 static int wf_ad7417_init(void)
334 /* This is only supported on these machines */
335 if (!of_machine_is_compatible("PowerMac7,2") &&
336 !of_machine_is_compatible("PowerMac7,3") &&
337 !of_machine_is_compatible("RackMac3,1"))
340 return i2c_add_driver(&wf_ad7417_driver
);
343 static void wf_ad7417_exit(void)
345 i2c_del_driver(&wf_ad7417_driver
);
348 module_init(wf_ad7417_init
);
349 module_exit(wf_ad7417_exit
);
351 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
352 MODULE_DESCRIPTION("ad7417 sensor driver for PowerMacs");
353 MODULE_LICENSE("GPL");