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