1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. AD7417 sensors
5 * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/i2c.h>
17 #include <asm/machdep.h>
19 #include <asm/sections.h>
22 #include "windfarm_mpu.h"
26 struct wf_ad7417_priv
{
28 struct i2c_client
*i2c
;
31 const struct mpu_data
*mpu
;
32 struct wf_sensor sensors
[5];
36 static int wf_ad7417_temp_get(struct wf_sensor
*sr
, s32
*value
)
38 struct wf_ad7417_priv
*pv
= sr
->priv
;
44 mutex_lock(&pv
->lock
);
46 /* Read temp register */
48 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
51 rc
= i2c_master_recv(pv
->i2c
, buf
, 2);
55 /* Read a a 16-bit signed value */
56 raw
= be16_to_cpup((__le16
*)buf
);
58 /* Convert 8.8-bit to 16.16 fixed point */
59 *value
= ((s32
)raw
) << 8;
61 mutex_unlock(&pv
->lock
);
65 mutex_unlock(&pv
->lock
);
70 * Scaling factors for the AD7417 ADC converters (except
71 * for the CPU diode which is obtained from the EEPROM).
72 * Those values are obtained from the property list of
75 #define ADC_12V_CURRENT_SCALE 0x0320 /* _AD2 */
76 #define ADC_CPU_VOLTAGE_SCALE 0x00a0 /* _AD3 */
77 #define ADC_CPU_CURRENT_SCALE 0x1f40 /* _AD4 */
79 static void wf_ad7417_adc_convert(struct wf_ad7417_priv
*pv
,
80 int chan
, s32 raw
, s32
*value
)
84 *value
= (raw
* (s32
)pv
->mpu
->mdiode
+
85 ((s32
)pv
->mpu
->bdiode
<< 12)) >> 2;
87 case 2: /* 12v current */
88 *value
= raw
* ADC_12V_CURRENT_SCALE
;
90 case 3: /* core voltage */
91 *value
= raw
* ADC_CPU_VOLTAGE_SCALE
;
93 case 4: /* core current */
94 *value
= raw
* ADC_CPU_CURRENT_SCALE
;
99 static int wf_ad7417_adc_get(struct wf_sensor
*sr
, s32
*value
)
101 struct wf_ad7417_priv
*pv
= sr
->priv
;
102 int chan
= sr
- pv
->sensors
;
108 mutex_lock(&pv
->lock
);
109 for (i
= 0; i
< 10; i
++) {
112 buf
[1] = (pv
->config
& 0x1f) | (chan
<< 5);
113 rc
= i2c_master_send(pv
->i2c
, buf
, 2);
117 /* Wait for conversion */
120 /* Switch to data register */
122 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
127 rc
= i2c_master_recv(pv
->i2c
, buf
, 2);
131 /* Read a a 16-bit signed value */
132 raw
= be16_to_cpup((__le16
*)buf
) >> 6;
133 wf_ad7417_adc_convert(pv
, chan
, raw
, value
);
135 dev_vdbg(&pv
->i2c
->dev
, "ADC chan %d [%s]"
136 " raw value: 0x%x, conv to: 0x%08x\n",
137 chan
, sr
->name
, raw
, *value
);
139 mutex_unlock(&pv
->lock
);
143 dev_dbg(&pv
->i2c
->dev
,
144 "Error reading ADC, try %d...\n", i
);
148 mutex_unlock(&pv
->lock
);
152 static void wf_ad7417_release(struct kref
*ref
)
154 struct wf_ad7417_priv
*pv
= container_of(ref
,
155 struct wf_ad7417_priv
, ref
);
159 static void wf_ad7417_sensor_release(struct wf_sensor
*sr
)
161 struct wf_ad7417_priv
*pv
= sr
->priv
;
164 kref_put(&pv
->ref
, wf_ad7417_release
);
167 static const struct wf_sensor_ops wf_ad7417_temp_ops
= {
168 .get_value
= wf_ad7417_temp_get
,
169 .release
= wf_ad7417_sensor_release
,
170 .owner
= THIS_MODULE
,
173 static const struct wf_sensor_ops wf_ad7417_adc_ops
= {
174 .get_value
= wf_ad7417_adc_get
,
175 .release
= wf_ad7417_sensor_release
,
176 .owner
= THIS_MODULE
,
179 static void wf_ad7417_add_sensor(struct wf_ad7417_priv
*pv
,
180 int index
, const char *name
,
181 const struct wf_sensor_ops
*ops
)
183 pv
->sensors
[index
].name
= kasprintf(GFP_KERNEL
, "%s-%d", name
, pv
->cpu
);
184 pv
->sensors
[index
].priv
= pv
;
185 pv
->sensors
[index
].ops
= ops
;
186 if (!wf_register_sensor(&pv
->sensors
[index
]))
190 static void wf_ad7417_init_chip(struct wf_ad7417_priv
*pv
)
197 * Read ADC the configuration register and cache it. We
198 * also make sure Config2 contains proper values, I've seen
199 * cases where we got stale grabage in there, thus preventing
200 * proper reading of conv. values
206 i2c_master_send(pv
->i2c
, buf
, 2);
208 /* Read & cache Config1 */
210 rc
= i2c_master_send(pv
->i2c
, buf
, 1);
212 rc
= i2c_master_recv(pv
->i2c
, buf
, 1);
216 dev_dbg(&pv
->i2c
->dev
, "ADC config reg: %02x\n",
219 /* Disable shutdown mode */
223 rc
= i2c_master_send(pv
->i2c
, buf
, 2);
227 dev_err(&pv
->i2c
->dev
, "Error reading ADC config\n");
232 static int wf_ad7417_probe(struct i2c_client
*client
,
233 const struct i2c_device_id
*id
)
235 struct wf_ad7417_priv
*pv
;
236 const struct mpu_data
*mpu
;
240 loc
= of_get_property(client
->dev
.of_node
, "hwsensor-location", NULL
);
242 dev_warn(&client
->dev
, "Missing hwsensor-location property!\n");
247 * Identify which CPU we belong to by looking at the first entry
248 * in the hwsensor-location list
250 if (!strncmp(loc
, "CPU A", 5))
252 else if (!strncmp(loc
, "CPU B", 5))
255 pr_err("wf_ad7417: Can't identify location %s\n", loc
);
258 mpu
= wf_get_mpu(cpu_nr
);
260 dev_err(&client
->dev
, "Failed to retrieve MPU data\n");
264 pv
= kzalloc(sizeof(struct wf_ad7417_priv
), GFP_KERNEL
);
269 mutex_init(&pv
->lock
);
273 dev_set_drvdata(&client
->dev
, pv
);
275 /* Initialize the chip */
276 wf_ad7417_init_chip(pv
);
279 * We cannot rely on Apple device-tree giving us child
280 * node with the names of the individual sensors so we
281 * just hard code what we know about them
283 wf_ad7417_add_sensor(pv
, 0, "cpu-amb-temp", &wf_ad7417_temp_ops
);
284 wf_ad7417_add_sensor(pv
, 1, "cpu-diode-temp", &wf_ad7417_adc_ops
);
285 wf_ad7417_add_sensor(pv
, 2, "cpu-12v-current", &wf_ad7417_adc_ops
);
286 wf_ad7417_add_sensor(pv
, 3, "cpu-voltage", &wf_ad7417_adc_ops
);
287 wf_ad7417_add_sensor(pv
, 4, "cpu-current", &wf_ad7417_adc_ops
);
292 static int wf_ad7417_remove(struct i2c_client
*client
)
294 struct wf_ad7417_priv
*pv
= dev_get_drvdata(&client
->dev
);
297 /* Mark client detached */
301 for (i
= 0; i
< 5; i
++)
302 wf_unregister_sensor(&pv
->sensors
[i
]);
304 kref_put(&pv
->ref
, wf_ad7417_release
);
309 static const struct i2c_device_id wf_ad7417_id
[] = {
313 MODULE_DEVICE_TABLE(i2c
, wf_ad7417_id
);
315 static const struct of_device_id wf_ad7417_of_id
[] = {
316 { .compatible
= "ad7417", },
319 MODULE_DEVICE_TABLE(of
, wf_ad7417_of_id
);
321 static struct i2c_driver wf_ad7417_driver
= {
324 .of_match_table
= wf_ad7417_of_id
,
326 .probe
= wf_ad7417_probe
,
327 .remove
= wf_ad7417_remove
,
328 .id_table
= wf_ad7417_id
,
331 static int wf_ad7417_init(void)
333 /* This is only supported on these machines */
334 if (!of_machine_is_compatible("PowerMac7,2") &&
335 !of_machine_is_compatible("PowerMac7,3") &&
336 !of_machine_is_compatible("RackMac3,1"))
339 return i2c_add_driver(&wf_ad7417_driver
);
342 static void wf_ad7417_exit(void)
344 i2c_del_driver(&wf_ad7417_driver
);
347 module_init(wf_ad7417_init
);
348 module_exit(wf_ad7417_exit
);
350 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
351 MODULE_DESCRIPTION("ad7417 sensor driver for PowerMacs");
352 MODULE_LICENSE("GPL");