xref: /titanic_52/usr/src/uts/sun4u/io/i2c/clients/lm75.c (revision 71269a2275bf5a143dad6461eee2710a344e7261)
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 	i2c_tran_pointer->i2c_flags = I2C_WR;
297 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
298 	i2c_tran_pointer->i2c_wbuf[1] = (temp16 >> 1);
299 	i2c_tran_pointer->i2c_wbuf[2] =	((temp16 & 0xFE) << 7);
300 
301 	err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
302 	i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
303 	return (err);
304 }
305 
306 static int
307 lm75_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
308 		int *rvalp)
309 {
310 	_NOTE(ARGUNUSED(credp, rvalp))
311 
312 	struct lm75_unit	*unitp;
313 	int			instance;
314 	int			err = 0;
315 	i2c_transfer_t		*i2c_tran_pointer;
316 	uchar_t			passin_byte;
317 
318 	if (arg == NULL) {
319 		D2CMN_ERR((CE_WARN, "LM75: ioctl: arg passed in to ioctl "
320 		    "= NULL\n"));
321 		err = EINVAL;
322 		return (err);
323 	}
324 	instance = getminor(dev);
325 	unitp = (struct lm75_unit *)
326 	    ddi_get_soft_state(lm75soft_statep, instance);
327 
328 	if (unitp == NULL) {
329 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
330 		err = ENOMEM;
331 		return (err);
332 	}
333 
334 	mutex_enter(&unitp->lm75_mutex);
335 
336 	switch (cmd) {
337 	case I2C_GET_TEMPERATURE:
338 		err = lm75_get16(arg, LM75_TEMPERATURE_REG, unitp, mode);
339 		break;
340 
341 	case LM75_GET_HYST:
342 		err = lm75_get16(arg, LM75_HYST_REG, unitp, mode);
343 		break;
344 
345 	case LM75_SET_HYST:
346 		err = lm75_set16(arg, LM75_HYST_REG, unitp, mode);
347 		break;
348 
349 	case LM75_GET_OVERTEMP_SHUTDOWN:
350 		err = lm75_get16(arg, LM75_OVERTEMP_REG, unitp, mode);
351 		break;
352 
353 	case LM75_SET_OVERTEMP_SHUTDOWN:
354 		err = lm75_set16(arg, LM75_OVERTEMP_REG, unitp, mode);
355 		break;
356 
357 	case LM75_GET_CONFIG:
358 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
359 		    1, 1, I2C_SLEEP);
360 		if (i2c_tran_pointer == NULL) {
361 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
362 			    "i2c_tran_pointer not allocated\n",
363 			    unitp->lm75_name));
364 			err = ENOMEM;
365 			break;
366 		}
367 		i2c_tran_pointer->i2c_flags = I2C_WR_RD;
368 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
369 
370 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
371 		if (err) {
372 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
373 			    "i2c_transfer routine\n",
374 			    unitp->lm75_name));
375 			i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
376 			break;
377 		}
378 		if (ddi_copyout((caddr_t)i2c_tran_pointer->i2c_rbuf,
379 		    (caddr_t)arg,
380 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
381 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
382 			    "ddi_copyout routine\n",
383 			    unitp->lm75_name));
384 			err = EFAULT;
385 		}
386 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
387 		break;
388 
389 	case LM75_SET_CONFIG:
390 		if (ddi_copyin((caddr_t)arg, (caddr_t)&passin_byte,
391 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
392 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
393 			    "ddi_copyin routine\n",
394 			    unitp->lm75_name));
395 			err = EFAULT;
396 			break;
397 		}
398 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
399 		    2, 0, I2C_SLEEP);
400 		if (i2c_tran_pointer == NULL) {
401 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
402 			    "i2c_tran_pointer not allocated\n",
403 			    unitp->lm75_name));
404 			err = ENOMEM;
405 			break;
406 		}
407 		i2c_tran_pointer->i2c_flags = I2C_WR;
408 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
409 		i2c_tran_pointer->i2c_wbuf[1] = passin_byte;
410 
411 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
412 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
413 		break;
414 
415 	default:
416 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd %x\n",
417 		    unitp->lm75_name, cmd));
418 		err = EINVAL;
419 	}
420 
421 	mutex_exit(&unitp->lm75_mutex);
422 	return (err);
423 }
424 
425 static int
426 lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
427 {
428 	switch (cmd) {
429 	case DDI_ATTACH:
430 		return (lm75_do_attach(dip));
431 	case DDI_RESUME:
432 		return (lm75_do_resume());
433 	default:
434 		return (DDI_FAILURE);
435 	}
436 }
437 
438 static int
439 lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
440 {
441 	switch (cmd) {
442 	case DDI_DETACH:
443 		return (lm75_do_detach(dip));
444 	case DDI_SUSPEND:
445 		return (lm75_do_suspend());
446 	default:
447 		return (DDI_FAILURE);
448 	}
449 }
450 
451 static int
452 lm75_do_attach(dev_info_t *dip)
453 {
454 	struct lm75_unit *unitp;
455 	int instance;
456 
457 	instance = ddi_get_instance(dip);
458 
459 	if (ddi_soft_state_zalloc(lm75soft_statep, instance) != 0) {
460 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
461 		    ddi_get_name(dip), instance);
462 		return (DDI_FAILURE);
463 	}
464 
465 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
466 
467 	if (unitp == NULL) {
468 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
469 		return (ENOMEM);
470 	}
471 
472 	(void) snprintf(unitp->lm75_name, sizeof (unitp->lm75_name),
473 	    "%s%d", ddi_node_name(dip), instance);
474 
475 	if (ddi_create_minor_node(dip, "lm75", S_IFCHR, instance,
476 	    "ddi_i2c:temperature_sensor", NULL) == DDI_FAILURE) {
477 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
478 		    "%s\n", unitp->lm75_name, "lm75");
479 		ddi_soft_state_free(lm75soft_statep, instance);
480 
481 		return (DDI_FAILURE);
482 	}
483 
484 	if (i2c_client_register(dip, &unitp->lm75_hdl) != I2C_SUCCESS) {
485 		ddi_remove_minor_node(dip, NULL);
486 		ddi_soft_state_free(lm75soft_statep, instance);
487 
488 		return (DDI_FAILURE);
489 	}
490 
491 	mutex_init(&unitp->lm75_mutex, NULL, MUTEX_DRIVER, NULL);
492 
493 	return (DDI_SUCCESS);
494 }
495 
496 static int
497 lm75_do_resume(void)
498 {
499 	int ret = DDI_SUCCESS;
500 
501 	return (ret);
502 }
503 
504 static int
505 lm75_do_suspend()
506 {
507 	int ret = DDI_SUCCESS;
508 
509 	return (ret);
510 }
511 
512 static int
513 lm75_do_detach(dev_info_t *dip)
514 {
515 	struct lm75_unit *unitp;
516 	int instance;
517 
518 	instance = ddi_get_instance(dip);
519 
520 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
521 
522 	if (unitp == NULL) {
523 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
524 		return (ENOMEM);
525 	}
526 
527 	i2c_client_unregister(unitp->lm75_hdl);
528 
529 	ddi_remove_minor_node(dip, NULL);
530 
531 	mutex_destroy(&unitp->lm75_mutex);
532 	ddi_soft_state_free(lm75soft_statep, instance);
533 
534 	return (DDI_SUCCESS);
535 }
536