1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Andreas Tobler
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/systm.h>
35#include <sys/module.h>
36#include <sys/callout.h>
37#include <sys/conf.h>
38#include <sys/cpu.h>
39#include <sys/ctype.h>
40#include <sys/kernel.h>
41#include <sys/reboot.h>
42#include <sys/rman.h>
43#include <sys/sysctl.h>
44#include <sys/limits.h>
45
46#include <machine/bus.h>
47#include <machine/md_var.h>
48
49#include <dev/iicbus/iicbus.h>
50#include <dev/iicbus/iiconf.h>
51
52#include <dev/ofw/openfirm.h>
53#include <dev/ofw/ofw_bus.h>
54#include <powerpc/powermac/powermac_thermal.h>
55
56/* Sensor: Maxim DS1631 */
57
58#define DS1631_STOP            0x22
59#define DS1631_START           0x51
60#define DS1631_RESET           0x54
61#define DS1631_TEMP            0xAA
62#define DS1631_CONTROL         0xAC
63#define DS1631_CONTROL_1SHOT   0x01
64#define DS1631_CONTROL_9BIT    0x00
65#define DS1631_CONTROL_10BIT   0x04
66#define DS1631_CONTROL_11BIT   0x08
67#define DS1631_CONTROL_12BIT   0x0C
68
69
70
71/* Regular bus attachment functions */
72static int  ds1631_probe(device_t);
73static int  ds1631_attach(device_t);
74
75struct ds1631_softc {
76	struct pmac_therm	sc_sensor;
77	device_t		sc_dev;
78	struct intr_config_hook enum_hook;
79	uint32_t                sc_addr;
80	uint32_t		init_done;
81};
82
83struct write_data {
84	uint8_t reg;
85	uint8_t val;
86};
87
88struct read_data {
89	uint8_t reg;
90	uint16_t val;
91};
92
93/* Utility functions */
94static int  ds1631_sensor_read(struct ds1631_softc *sc);
95static int  ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS);
96static void ds1631_start(void *xdev);
97static int  ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg,
98			  uint8_t *data);
99static int  ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg,
100			  uint16_t *data);
101static int  ds1631_write(device_t dev, uint32_t addr, uint8_t reg,
102			 uint8_t *buff, int len);
103
104static device_method_t  ds1631_methods[] = {
105	/* Device interface */
106	DEVMETHOD(device_probe,		ds1631_probe),
107	DEVMETHOD(device_attach,	ds1631_attach),
108	{ 0, 0 },
109};
110
111static driver_t ds1631_driver = {
112	"ds1631",
113	ds1631_methods,
114	sizeof(struct ds1631_softc)
115};
116
117static devclass_t ds1631_devclass;
118
119DRIVER_MODULE(ds1631, iicbus, ds1631_driver, ds1631_devclass, 0, 0);
120
121static int
122ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len)
123{
124	uint8_t buf[4];
125	int try = 0;
126
127	struct iic_msg msg[] = {
128		{ addr, IIC_M_WR, 0, buf }
129	};
130
131	/* Prepare the write msg. */
132	msg[0].len = len + 1;
133	buf[0] = reg;
134	memcpy(buf + 1, buff, len);
135
136	for (;;)
137	{
138		if (iicbus_transfer(dev, msg, 1) == 0)
139			return (0);
140		if (++try > 5) {
141			device_printf(dev, "iicbus write failed\n");
142			return (-1);
143		}
144		pause("ds1631_write", hz);
145	}
146}
147
148static int
149ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data)
150{
151	uint8_t buf[4];
152	int err, try = 0;
153
154	struct iic_msg msg[2] = {
155		{ addr, IIC_M_WR, 1, &reg },
156		{ addr, IIC_M_RD, 1, buf },
157	};
158
159	for (;;)
160	{
161		err = iicbus_transfer(dev, msg, 2);
162		if (err != 0)
163			goto retry;
164
165		*data = *((uint8_t*)buf);
166		return (0);
167	retry:
168		if (++try > 5) {
169			device_printf(dev, "iicbus read failed\n");
170			return (-1);
171		}
172		pause("ds1631_read_1", hz);
173	}
174}
175
176static int
177ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data)
178{
179	uint8_t buf[4];
180	int err, try = 0;
181
182	struct iic_msg msg[2] = {
183		{ addr, IIC_M_WR, 1, &reg },
184		{ addr, IIC_M_RD, 2, buf },
185	};
186
187	for (;;)
188	{
189		err = iicbus_transfer(dev, msg, 2);
190		if (err != 0)
191			goto retry;
192
193		*data = *((uint16_t*)buf);
194		return (0);
195	retry:
196		if (++try > 5) {
197			device_printf(dev, "iicbus read failed\n");
198			return (-1);
199		}
200		pause("ds1631_read_2", hz);
201	}
202}
203
204static int
205ds1631_probe(device_t dev)
206{
207	const char  *name, *compatible;
208	struct ds1631_softc *sc;
209
210	name = ofw_bus_get_name(dev);
211	compatible = ofw_bus_get_compat(dev);
212
213	if (!name)
214		return (ENXIO);
215
216	if (strcmp(name, "temp-monitor") != 0 ||
217	    strcmp(compatible, "ds1631") != 0 )
218		return (ENXIO);
219
220	sc = device_get_softc(dev);
221	sc->sc_dev = dev;
222	sc->sc_addr = iicbus_get_addr(dev);
223
224	device_set_desc(dev, "Temp-Monitor DS1631");
225
226	return (0);
227}
228
229static int
230ds1631_attach(device_t dev)
231{
232	struct ds1631_softc *sc;
233
234	sc = device_get_softc(dev);
235
236	sc->enum_hook.ich_func = ds1631_start;
237	sc->enum_hook.ich_arg = dev;
238
239	/*
240	 * We have to wait until interrupts are enabled. I2C read and write
241	 * only works if the interrupts are available.
242	 * The unin/i2c is controlled by the htpic on unin. But this is not
243	 * the master. The openpic on mac-io is controlling the htpic.
244	 * This one gets attached after the mac-io probing and then the
245	 * interrupts will be available.
246	 */
247
248	if (config_intrhook_establish(&sc->enum_hook) != 0)
249		return (ENOMEM);
250
251	return (0);
252}
253static int
254ds1631_init(device_t dev, uint32_t addr)
255{
256	uint8_t conf;
257	int err;
258	struct ds1631_softc *sc;
259
260	sc = device_get_softc(dev);
261
262	err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf);
263	if (err < 0) {
264		device_printf(dev, "ds1631 read config failed: %x\n", err);
265		return (-1);
266	}
267
268	/* Stop the conversion if not in 1SHOT mode. */
269	if (conf & ~DS1631_CONTROL_1SHOT)
270		err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0);
271
272	/*
273	 * Setup the resolution, 10-bit is enough. Each bit increase in
274	 * resolution doubles the conversion time.
275	 */
276	conf = DS1631_CONTROL_10BIT;
277
278	err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, 1);
279	if (err < 0) {
280		device_printf(dev, "ds1631 write config failed: %x\n", err);
281		return (-1);
282	}
283
284	/* And now start....*/
285	err = ds1631_write(dev, addr, DS1631_START, &conf, 0);
286
287	if (err < 0) {
288		device_printf(dev, "ds1631 write start failed: %x\n", err);
289		return (-1);
290	}
291
292	sc->init_done = 1;
293
294	return (0);
295
296}
297static void
298ds1631_start(void *xdev)
299{
300	phandle_t child, node;
301	struct ds1631_softc *sc;
302	struct sysctl_oid *oid, *sensroot_oid;
303	struct sysctl_ctx_list *ctx;
304	ssize_t plen;
305	int i;
306	char  sysctl_desc[40], sysctl_name[40];
307
308	device_t dev = (device_t)xdev;
309
310	sc = device_get_softc(dev);
311
312	child = ofw_bus_get_node(dev);
313
314	ctx = device_get_sysctl_ctx(dev);
315	sensroot_oid = SYSCTL_ADD_NODE(ctx,
316	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor",
317	    CTLFLAG_RD, 0, "DS1631 Sensor Information");
318
319	if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone,
320		       sizeof(int)) < 0)
321		sc->sc_sensor.zone = 0;
322
323	plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name,
324			  sizeof(sc->sc_sensor.name));
325	if (plen == -1) {
326		/*
327		 * Ok, no hwsensor-location property, so let's look for a
328		 * location property on a sub node.
329		 */
330		for (node = OF_child(child); node; node = OF_peer(node))
331			plen = OF_getprop(node, "location", sc->sc_sensor.name,
332					  sizeof(sc->sc_sensor.name));
333	}
334
335	if (plen == -1) {
336		strcpy(sysctl_name, "sensor");
337	} else {
338		for (i = 0; i < strlen(sc->sc_sensor.name); i++) {
339			sysctl_name[i] = tolower(sc->sc_sensor.name[i]);
340			if (isspace(sysctl_name[i]))
341				sysctl_name[i] = '_';
342		}
343		sysctl_name[i] = 0;
344	}
345
346	/* Make up target temperatures. These are low, for the drive bay. */
347	if (sc->sc_sensor.zone == 0) {
348		sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K;
349		sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
350	} else {
351		sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K;
352		sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K;
353	}
354
355	sc->sc_sensor.read =
356	    (int (*)(struct pmac_therm *sc))(ds1631_sensor_read);
357	pmac_thermal_sensor_register(&sc->sc_sensor);
358
359	sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)");
360	oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid),
361			      OID_AUTO, sysctl_name, CTLFLAG_RD, 0,
362			      "Sensor Information");
363	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp",
364			CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
365			0, ds1631_sensor_sysctl, "IK", sysctl_desc);
366
367	config_intrhook_disestablish(&sc->enum_hook);
368}
369
370static int
371ds1631_sensor_read(struct ds1631_softc *sc)
372{
373	uint16_t buf[2];
374	uint16_t read;
375	int err;
376
377	if (!sc->init_done)
378		ds1631_init(sc->sc_dev, sc->sc_addr);
379
380	err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf);
381	if (err < 0) {
382		device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err);
383		return (-1);
384	}
385
386	read = *((int16_t *)buf);
387
388	/*
389	 * The default mode of the ADC is 12-bit, the resolution is 0.0625 C
390	 * per bit. The temperature is in tenth kelvin.
391	 * We use 10-bit resolution which seems enough, resolution is 0.25 C.
392	 */
393
394	return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K);
395}
396
397static int
398ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS)
399{
400	device_t dev;
401	struct ds1631_softc *sc;
402	int error;
403	int temp;
404
405	dev = arg1;
406	sc = device_get_softc(dev);
407
408	temp = ds1631_sensor_read(sc);
409	if (temp < 0)
410		return (EIO);
411
412	error = sysctl_handle_int(oidp, &temp, 0, req);
413
414	return (error);
415}
416