xref: /illumos-gate/usr/src/uts/sun4u/io/i2c/clients/lm75.c (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
27 #include <sys/modctl.h>		/* for modldrv */
28 #include <sys/open.h>		/* for open params.	 */
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/sunddi.h>
32 #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
33 #include <sys/ddi.h>
34 #include <sys/file.h>
35 #include <sys/note.h>
36 #include <sys/i2c/clients/lm75.h>
37 #include <sys/i2c/clients/lm75_impl.h>
38 
39 static void *lm75soft_statep;
40 
41 static int lm75_do_attach(dev_info_t *);
42 static int lm75_do_detach(dev_info_t *);
43 static int lm75_do_resume(void);
44 static int lm75_do_suspend(void);
45 static int lm75_get16(intptr_t, int, struct lm75_unit *, int);
46 static int lm75_set16(intptr_t, int, struct lm75_unit *, int);
47 
48 /*
49  * cb ops (only need ioctl)
50  */
51 static int lm75_open(dev_t *, int, int, cred_t *);
52 static int lm75_close(dev_t, int, int, cred_t *);
53 static int lm75_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
54 
55 static struct cb_ops lm75_cbops = {
56 	lm75_open,			/* open  */
57 	lm75_close,			/* close */
58 	nodev,				/* strategy */
59 	nodev,				/* print */
60 	nodev,				/* dump */
61 	nodev,				/* read */
62 	nodev,				/* write */
63 	lm75_ioctl,			/* ioctl */
64 	nodev,				/* devmap */
65 	nodev,				/* mmap */
66 	nodev,				/* segmap */
67 	nochpoll,			/* poll */
68 	ddi_prop_op,			/* cb_prop_op */
69 	NULL,				/* streamtab */
70 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
71 	CB_REV,				/* rev */
72 	nodev,				/* int (*cb_aread)() */
73 	nodev				/* int (*cb_awrite)() */
74 };
75 
76 /*
77  * dev ops
78  */
79 static int lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
80 static int lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
81 
82 static struct dev_ops lm75_ops = {
83 	DEVO_REV,
84 	0,
85 	ddi_getinfo_1to1,
86 	nulldev,
87 	nulldev,
88 	lm75_attach,
89 	lm75_detach,
90 	nodev,
91 	&lm75_cbops,
92 	NULL,
93 	NULL,
94 	ddi_quiesce_not_needed,		/* quiesce */
95 };
96 
97 extern struct mod_ops mod_driverops;
98 
99 static struct modldrv lm75_modldrv = {
100 	&mod_driverops,			/* type of module - driver */
101 	"LM75 i2c device driver",
102 	&lm75_ops
103 };
104 
105 static struct modlinkage lm75_modlinkage = {
106 	MODREV_1,
107 	&lm75_modldrv,
108 	0
109 };
110 
111 
112 int
113 _init(void)
114 {
115 	int error;
116 
117 	error = mod_install(&lm75_modlinkage);
118 
119 	if (!error)
120 		(void) ddi_soft_state_init(&lm75soft_statep,
121 		    sizeof (struct lm75_unit), 1);
122 	return (error);
123 }
124 
125 int
126 _fini(void)
127 {
128 	int error;
129 
130 	error = mod_remove(&lm75_modlinkage);
131 	if (!error)
132 		ddi_soft_state_fini(&lm75soft_statep);
133 
134 	return (error);
135 }
136 
137 int
138 _info(struct modinfo *modinfop)
139 {
140 	return (mod_info(&lm75_modlinkage, modinfop));
141 }
142 
143 static int
144 lm75_open(dev_t *devp, int flags, int otyp, cred_t *credp)
145 {
146 	_NOTE(ARGUNUSED(credp))
147 
148 	struct lm75_unit *unitp;
149 	int instance;
150 	int error = 0;
151 
152 	instance = getminor(*devp);
153 
154 	if (instance < 0) {
155 		return (ENXIO);
156 	}
157 
158 	unitp = (struct lm75_unit *)
159 	    ddi_get_soft_state(lm75soft_statep, instance);
160 
161 	if (unitp == NULL) {
162 		return (ENXIO);
163 	}
164 
165 	if (otyp != OTYP_CHR) {
166 		return (EINVAL);
167 	}
168 
169 	mutex_enter(&unitp->lm75_mutex);
170 
171 	if (flags & FEXCL) {
172 		if (unitp->lm75_oflag != 0) {
173 			error = EBUSY;
174 		} else {
175 			unitp->lm75_oflag = FEXCL;
176 		}
177 	} else {
178 		if (unitp->lm75_oflag == FEXCL) {
179 			error = EBUSY;
180 		} else {
181 			unitp->lm75_oflag = FOPEN;
182 		}
183 	}
184 
185 	mutex_exit(&unitp->lm75_mutex);
186 
187 	return (error);
188 }
189 
190 static int
191 lm75_close(dev_t dev, int flags, int otyp, cred_t *credp)
192 {
193 	_NOTE(ARGUNUSED(flags, otyp, credp))
194 
195 	struct lm75_unit *unitp;
196 	int instance;
197 
198 	instance = getminor(dev);
199 
200 	if (instance < 0) {
201 		return (ENXIO);
202 	}
203 
204 	unitp = (struct lm75_unit *)
205 	    ddi_get_soft_state(lm75soft_statep, instance);
206 
207 	if (unitp == NULL) {
208 		return (ENXIO);
209 	}
210 
211 	mutex_enter(&unitp->lm75_mutex);
212 
213 	unitp->lm75_oflag = 0;
214 
215 	mutex_exit(&unitp->lm75_mutex);
216 	return (DDI_SUCCESS);
217 }
218 
219 static int
220 lm75_get16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode) {
221 	i2c_transfer_t		*i2c_tran_pointer;
222 	int err = DDI_SUCCESS;
223 	int16_t			temp16;
224 	int8_t			holder;
225 
226 	(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
227 				1, 2, I2C_SLEEP);
228 	if (i2c_tran_pointer == NULL) {
229 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
230 				"i2c_tran_pointer not allocated\n",
231 				unitp->lm75_name));
232 		return (ENOMEM);
233 	}
234 
235 	i2c_tran_pointer->i2c_flags = I2C_WR_RD;
236 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
237 
238 	err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
239 	if (err) {
240 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
241 				"i2c_transfer routine\n",
242 				unitp->lm75_name));
243 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
244 		return (err);
245 	}
246 
247 	D1CMN_ERR((CE_NOTE, "%s: rbuf[0] =  %x rbuf[1] = %x\n",
248 		unitp->lm75_name, i2c_tran_pointer->i2c_rbuf[0],
249 		i2c_tran_pointer->i2c_rbuf[0]));
250 	temp16 = i2c_tran_pointer->i2c_rbuf[0];
251 	temp16 = (temp16 << 1);
252 	temp16 = (temp16 | ((i2c_tran_pointer->i2c_rbuf[1] & 0x80) >> 7));
253 
254 
255 	if (temp16 & LM75_COMP_MASK) {
256 		holder = (temp16 & LM75_COMP_MASK_UPPER);
257 		holder = -holder;
258 		holder = holder/2;
259 		temp16 = 0 - holder;
260 	} else {
261 		temp16 = temp16 / 2;
262 	}
263 	if (ddi_copyout((caddr_t)&temp16, (caddr_t)arg,
264 			sizeof (int16_t), mode) != DDI_SUCCESS) {
265 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
266 				"ddi_copyout routine\n",
267 				unitp->lm75_name));
268 		err = EFAULT;
269 	}
270 	i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
271 	return (err);
272 }
273 
274 static int
275 lm75_set16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode) {
276 	i2c_transfer_t		*i2c_tran_pointer;
277 	int err = DDI_SUCCESS;
278 	int16_t			temp16;
279 
280 	if (ddi_copyin((caddr_t)arg, (caddr_t)&temp16,
281 			sizeof (int16_t), mode) != DDI_SUCCESS) {
282 		D2CMN_ERR((CE_WARN, "%s: Failed in LM74_SET_HYST "
283 				"ddi_copyin routine\n",
284 				unitp->lm75_name));
285 		return (EFAULT);
286 	}
287 
288 	(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
289 				3, 0, I2C_SLEEP);
290 	if (i2c_tran_pointer == NULL) {
291 		D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_HYST "
292 				"i2c_tran_pointer not allocated\n",
293 				unitp->lm75_name));
294 		return (ENOMEM);
295 	}
296 
297 	/* BEGIN CSTYLED */
298 	/*
299 	 * The temperature is 16bits where the top 9 are a twos-complement
300 	 * word with the the least significant bit used to indicate 0.5C
301 	 *
302 	 * |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
303 	 * |-----------------------------------------------|
304 	 * |+-|	     Temperature      |	      Unused       |
305 	 */
306 	/* END CSTYLED */
307 	i2c_tran_pointer->i2c_flags = I2C_WR;
308 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
309 	i2c_tran_pointer->i2c_wbuf[1] = ((temp16 >> 1) & 0xff);
310 	i2c_tran_pointer->i2c_wbuf[2] = ((temp16 & 0x1) << 7);
311 
312 	err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
313 	i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
314 	return (err);
315 }
316 
317 static int
318 lm75_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
319 		int *rvalp)
320 {
321 	_NOTE(ARGUNUSED(credp, rvalp))
322 
323 	struct lm75_unit	*unitp;
324 	int			instance;
325 	int			err = 0;
326 	i2c_transfer_t		*i2c_tran_pointer;
327 	uchar_t			passin_byte;
328 
329 	if (arg == NULL) {
330 		D2CMN_ERR((CE_WARN, "LM75: ioctl: arg passed in to ioctl "
331 		    "= NULL\n"));
332 		err = EINVAL;
333 		return (err);
334 	}
335 	instance = getminor(dev);
336 	unitp = (struct lm75_unit *)
337 	    ddi_get_soft_state(lm75soft_statep, instance);
338 
339 	if (unitp == NULL) {
340 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
341 		err = ENOMEM;
342 		return (err);
343 	}
344 
345 	mutex_enter(&unitp->lm75_mutex);
346 
347 	switch (cmd) {
348 	case I2C_GET_TEMPERATURE:
349 		err = lm75_get16(arg, LM75_TEMPERATURE_REG, unitp, mode);
350 		break;
351 
352 	case LM75_GET_HYST:
353 		err = lm75_get16(arg, LM75_HYST_REG, unitp, mode);
354 		break;
355 
356 	case LM75_SET_HYST:
357 		err = lm75_set16(arg, LM75_HYST_REG, unitp, mode);
358 		break;
359 
360 	case LM75_GET_OVERTEMP_SHUTDOWN:
361 		err = lm75_get16(arg, LM75_OVERTEMP_REG, unitp, mode);
362 		break;
363 
364 	case LM75_SET_OVERTEMP_SHUTDOWN:
365 		err = lm75_set16(arg, LM75_OVERTEMP_REG, unitp, mode);
366 		break;
367 
368 	case LM75_GET_CONFIG:
369 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
370 		    1, 1, I2C_SLEEP);
371 		if (i2c_tran_pointer == NULL) {
372 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
373 			    "i2c_tran_pointer not allocated\n",
374 			    unitp->lm75_name));
375 			err = ENOMEM;
376 			break;
377 		}
378 		i2c_tran_pointer->i2c_flags = I2C_WR_RD;
379 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
380 
381 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
382 		if (err) {
383 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
384 			    "i2c_transfer routine\n",
385 			    unitp->lm75_name));
386 			i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
387 			break;
388 		}
389 		if (ddi_copyout((caddr_t)i2c_tran_pointer->i2c_rbuf,
390 		    (caddr_t)arg,
391 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
392 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
393 			    "ddi_copyout routine\n",
394 			    unitp->lm75_name));
395 			err = EFAULT;
396 		}
397 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
398 		break;
399 
400 	case LM75_SET_CONFIG:
401 		if (ddi_copyin((caddr_t)arg, (caddr_t)&passin_byte,
402 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
403 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
404 			    "ddi_copyin routine\n",
405 			    unitp->lm75_name));
406 			err = EFAULT;
407 			break;
408 		}
409 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
410 		    2, 0, I2C_SLEEP);
411 		if (i2c_tran_pointer == NULL) {
412 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
413 			    "i2c_tran_pointer not allocated\n",
414 			    unitp->lm75_name));
415 			err = ENOMEM;
416 			break;
417 		}
418 		i2c_tran_pointer->i2c_flags = I2C_WR;
419 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
420 		i2c_tran_pointer->i2c_wbuf[1] = passin_byte;
421 
422 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
423 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
424 		break;
425 
426 	default:
427 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd %x\n",
428 		    unitp->lm75_name, cmd));
429 		err = EINVAL;
430 	}
431 
432 	mutex_exit(&unitp->lm75_mutex);
433 	return (err);
434 }
435 
436 static int
437 lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
438 {
439 	switch (cmd) {
440 	case DDI_ATTACH:
441 		return (lm75_do_attach(dip));
442 	case DDI_RESUME:
443 		return (lm75_do_resume());
444 	default:
445 		return (DDI_FAILURE);
446 	}
447 }
448 
449 static int
450 lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
451 {
452 	switch (cmd) {
453 	case DDI_DETACH:
454 		return (lm75_do_detach(dip));
455 	case DDI_SUSPEND:
456 		return (lm75_do_suspend());
457 	default:
458 		return (DDI_FAILURE);
459 	}
460 }
461 
462 static int
463 lm75_do_attach(dev_info_t *dip)
464 {
465 	struct lm75_unit *unitp;
466 	int instance;
467 
468 	instance = ddi_get_instance(dip);
469 
470 	if (ddi_soft_state_zalloc(lm75soft_statep, instance) != 0) {
471 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
472 		    ddi_get_name(dip), instance);
473 		return (DDI_FAILURE);
474 	}
475 
476 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
477 
478 	if (unitp == NULL) {
479 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
480 		return (ENOMEM);
481 	}
482 
483 	(void) snprintf(unitp->lm75_name, sizeof (unitp->lm75_name),
484 	    "%s%d", ddi_node_name(dip), instance);
485 
486 	if (ddi_create_minor_node(dip, "lm75", S_IFCHR, instance,
487 	    "ddi_i2c:temperature_sensor", NULL) == DDI_FAILURE) {
488 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
489 		    "%s\n", unitp->lm75_name, "lm75");
490 		ddi_soft_state_free(lm75soft_statep, instance);
491 
492 		return (DDI_FAILURE);
493 	}
494 
495 	if (i2c_client_register(dip, &unitp->lm75_hdl) != I2C_SUCCESS) {
496 		ddi_remove_minor_node(dip, NULL);
497 		ddi_soft_state_free(lm75soft_statep, instance);
498 
499 		return (DDI_FAILURE);
500 	}
501 
502 	mutex_init(&unitp->lm75_mutex, NULL, MUTEX_DRIVER, NULL);
503 
504 	return (DDI_SUCCESS);
505 }
506 
507 static int
508 lm75_do_resume(void)
509 {
510 	int ret = DDI_SUCCESS;
511 
512 	return (ret);
513 }
514 
515 static int
516 lm75_do_suspend()
517 {
518 	int ret = DDI_SUCCESS;
519 
520 	return (ret);
521 }
522 
523 static int
524 lm75_do_detach(dev_info_t *dip)
525 {
526 	struct lm75_unit *unitp;
527 	int instance;
528 
529 	instance = ddi_get_instance(dip);
530 
531 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
532 
533 	if (unitp == NULL) {
534 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
535 		return (ENOMEM);
536 	}
537 
538 	i2c_client_unregister(unitp->lm75_hdl);
539 
540 	ddi_remove_minor_node(dip, NULL);
541 
542 	mutex_destroy(&unitp->lm75_mutex);
543 	ddi_soft_state_free(lm75soft_statep, instance);
544 
545 	return (DDI_SUCCESS);
546 }
547