xref: /illumos-gate/usr/src/uts/sun4u/io/i2c/clients/lm75.c (revision 54dcb32c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23f47a9c50Smathue  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
277c478bd9Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
287c478bd9Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/lm75.h>
377c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/lm75_impl.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void *lm75soft_statep;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int lm75_do_attach(dev_info_t *);
427c478bd9Sstevel@tonic-gate static int lm75_do_detach(dev_info_t *);
437c478bd9Sstevel@tonic-gate static int lm75_do_resume(void);
447c478bd9Sstevel@tonic-gate static int lm75_do_suspend(void);
457c478bd9Sstevel@tonic-gate static int lm75_get16(intptr_t, int, struct lm75_unit *, int);
467c478bd9Sstevel@tonic-gate static int lm75_set16(intptr_t, int, struct lm75_unit *, int);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * cb ops (only need ioctl)
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static int lm75_open(dev_t *, int, int, cred_t *);
527c478bd9Sstevel@tonic-gate static int lm75_close(dev_t, int, int, cred_t *);
537c478bd9Sstevel@tonic-gate static int lm75_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static struct cb_ops lm75_cbops = {
567c478bd9Sstevel@tonic-gate 	lm75_open,			/* open  */
577c478bd9Sstevel@tonic-gate 	lm75_close,			/* close */
587c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
597c478bd9Sstevel@tonic-gate 	nodev,				/* print */
607c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
617c478bd9Sstevel@tonic-gate 	nodev,				/* read */
627c478bd9Sstevel@tonic-gate 	nodev,				/* write */
637c478bd9Sstevel@tonic-gate 	lm75_ioctl,			/* ioctl */
647c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
657c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
667c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
677c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
687c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
697c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
707c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
717c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
727c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
737c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * dev ops
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate static int lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
807c478bd9Sstevel@tonic-gate static int lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static struct dev_ops lm75_ops = {
837c478bd9Sstevel@tonic-gate 	DEVO_REV,
847c478bd9Sstevel@tonic-gate 	0,
857c478bd9Sstevel@tonic-gate 	ddi_getinfo_1to1,
867c478bd9Sstevel@tonic-gate 	nulldev,
877c478bd9Sstevel@tonic-gate 	nulldev,
887c478bd9Sstevel@tonic-gate 	lm75_attach,
897c478bd9Sstevel@tonic-gate 	lm75_detach,
907c478bd9Sstevel@tonic-gate 	nodev,
917c478bd9Sstevel@tonic-gate 	&lm75_cbops,
9219397407SSherry Moore 	NULL,
9319397407SSherry Moore 	NULL,
9419397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static struct modldrv lm75_modldrv = {
1007c478bd9Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
10188294e09SRichard Bean 	"LM75 i2c device driver",
1027c478bd9Sstevel@tonic-gate 	&lm75_ops
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static struct modlinkage lm75_modlinkage = {
1067c478bd9Sstevel@tonic-gate 	MODREV_1,
1077c478bd9Sstevel@tonic-gate 	&lm75_modldrv,
1087c478bd9Sstevel@tonic-gate 	0
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate int
_init(void)1137c478bd9Sstevel@tonic-gate _init(void)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	int error;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	error = mod_install(&lm75_modlinkage);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (!error)
1207c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&lm75soft_statep,
12119397407SSherry Moore 		    sizeof (struct lm75_unit), 1);
1227c478bd9Sstevel@tonic-gate 	return (error);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate int
_fini(void)1267c478bd9Sstevel@tonic-gate _fini(void)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	int error;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	error = mod_remove(&lm75_modlinkage);
1317c478bd9Sstevel@tonic-gate 	if (!error)
1327c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&lm75soft_statep);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (error);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	return (mod_info(&lm75_modlinkage, modinfop));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static int
lm75_open(dev_t * devp,int flags,int otyp,cred_t * credp)1447c478bd9Sstevel@tonic-gate lm75_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	struct lm75_unit *unitp;
1497c478bd9Sstevel@tonic-gate 	int instance;
1507c478bd9Sstevel@tonic-gate 	int error = 0;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (instance < 0) {
1557c478bd9Sstevel@tonic-gate 		return (ENXIO);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	unitp = (struct lm75_unit *)
15919397407SSherry Moore 	    ddi_get_soft_state(lm75soft_statep, instance);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
1627c478bd9Sstevel@tonic-gate 		return (ENXIO);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1667c478bd9Sstevel@tonic-gate 		return (EINVAL);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->lm75_mutex);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
1727c478bd9Sstevel@tonic-gate 		if (unitp->lm75_oflag != 0) {
1737c478bd9Sstevel@tonic-gate 			error = EBUSY;
1747c478bd9Sstevel@tonic-gate 		} else {
1757c478bd9Sstevel@tonic-gate 			unitp->lm75_oflag = FEXCL;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	} else {
1787c478bd9Sstevel@tonic-gate 		if (unitp->lm75_oflag == FEXCL) {
1797c478bd9Sstevel@tonic-gate 			error = EBUSY;
1807c478bd9Sstevel@tonic-gate 		} else {
1817c478bd9Sstevel@tonic-gate 			unitp->lm75_oflag = FOPEN;
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->lm75_mutex);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (error);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static int
lm75_close(dev_t dev,int flags,int otyp,cred_t * credp)1917c478bd9Sstevel@tonic-gate lm75_close(dev_t dev, int flags, int otyp, cred_t *credp)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	struct lm75_unit *unitp;
1967c478bd9Sstevel@tonic-gate 	int instance;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (instance < 0) {
2017c478bd9Sstevel@tonic-gate 		return (ENXIO);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	unitp = (struct lm75_unit *)
20519397407SSherry Moore 	    ddi_get_soft_state(lm75soft_statep, instance);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2087c478bd9Sstevel@tonic-gate 		return (ENXIO);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->lm75_mutex);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	unitp->lm75_oflag = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->lm75_mutex);
2167c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static int
lm75_get16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)220*54dcb32cSToomas Soome lm75_get16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode)
221*54dcb32cSToomas Soome {
2227c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2237c478bd9Sstevel@tonic-gate 	int err = DDI_SUCCESS;
2247c478bd9Sstevel@tonic-gate 	int16_t			temp16;
2257c478bd9Sstevel@tonic-gate 	int8_t			holder;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
228*54dcb32cSToomas Soome 	    1, 2, I2C_SLEEP);
2297c478bd9Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2307c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
231*54dcb32cSToomas Soome 		    "i2c_tran_pointer not allocated\n", unitp->lm75_name));
2327c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR_RD;
2367c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
2397c478bd9Sstevel@tonic-gate 	if (err) {
2407c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
241*54dcb32cSToomas Soome 		    "i2c_transfer routine\n", unitp->lm75_name));
2427c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
2437c478bd9Sstevel@tonic-gate 		return (err);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: rbuf[0] =  %x rbuf[1] = %x\n",
247*54dcb32cSToomas Soome 	    unitp->lm75_name, i2c_tran_pointer->i2c_rbuf[0],
248*54dcb32cSToomas Soome 	    i2c_tran_pointer->i2c_rbuf[0]));
2497c478bd9Sstevel@tonic-gate 	temp16 = i2c_tran_pointer->i2c_rbuf[0];
2507c478bd9Sstevel@tonic-gate 	temp16 = (temp16 << 1);
2517c478bd9Sstevel@tonic-gate 	temp16 = (temp16 | ((i2c_tran_pointer->i2c_rbuf[1] & 0x80) >> 7));
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (temp16 & LM75_COMP_MASK) {
2557c478bd9Sstevel@tonic-gate 		holder = (temp16 & LM75_COMP_MASK_UPPER);
2567c478bd9Sstevel@tonic-gate 		holder = -holder;
2577c478bd9Sstevel@tonic-gate 		holder = holder/2;
2587c478bd9Sstevel@tonic-gate 		temp16 = 0 - holder;
2597c478bd9Sstevel@tonic-gate 	} else {
2607c478bd9Sstevel@tonic-gate 		temp16 = temp16 / 2;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&temp16, (caddr_t)arg,
263*54dcb32cSToomas Soome 	    sizeof (int16_t), mode) != DDI_SUCCESS) {
2647c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
265*54dcb32cSToomas Soome 		    "ddi_copyout routine\n", unitp->lm75_name));
2667c478bd9Sstevel@tonic-gate 		err = EFAULT;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
2697c478bd9Sstevel@tonic-gate 	return (err);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static int
lm75_set16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)273*54dcb32cSToomas Soome lm75_set16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode)
274*54dcb32cSToomas Soome {
2757c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2767c478bd9Sstevel@tonic-gate 	int err = DDI_SUCCESS;
2777c478bd9Sstevel@tonic-gate 	int16_t			temp16;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (ddi_copyin((caddr_t)arg, (caddr_t)&temp16,
280*54dcb32cSToomas Soome 	    sizeof (int16_t), mode) != DDI_SUCCESS) {
2817c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in LM74_SET_HYST "
282*54dcb32cSToomas Soome 		    "ddi_copyin routine\n", unitp->lm75_name));
2837c478bd9Sstevel@tonic-gate 		return (EFAULT);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
287*54dcb32cSToomas Soome 	    3, 0, I2C_SLEEP);
2887c478bd9Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2897c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_HYST "
290*54dcb32cSToomas Soome 		    "i2c_tran_pointer not allocated\n", unitp->lm75_name));
2917c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2927c478bd9Sstevel@tonic-gate 	}
293686928d4SRichard Lowe 
294686928d4SRichard Lowe 	/* BEGIN CSTYLED */
295686928d4SRichard Lowe 	/*
296686928d4SRichard Lowe 	 * The temperature is 16bits where the top 9 are a twos-complement
297686928d4SRichard Lowe 	 * word with the the least significant bit used to indicate 0.5C
298686928d4SRichard Lowe 	 *
299686928d4SRichard Lowe 	 * |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
300686928d4SRichard Lowe 	 * |-----------------------------------------------|
301686928d4SRichard Lowe 	 * |+-|	     Temperature      |	      Unused       |
302686928d4SRichard Lowe 	 */
303686928d4SRichard Lowe 	/* END CSTYLED */
3047c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
3057c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
306686928d4SRichard Lowe 	i2c_tran_pointer->i2c_wbuf[1] = ((temp16 >> 1) & 0xff);
307686928d4SRichard Lowe 	i2c_tran_pointer->i2c_wbuf[2] = ((temp16 & 0x1) << 7);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
3107c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3117c478bd9Sstevel@tonic-gate 	return (err);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static int
lm75_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)3157c478bd9Sstevel@tonic-gate lm75_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
316*54dcb32cSToomas Soome     int *rvalp)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	struct lm75_unit	*unitp;
3217c478bd9Sstevel@tonic-gate 	int			instance;
3227c478bd9Sstevel@tonic-gate 	int			err = 0;
3237c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
3247c478bd9Sstevel@tonic-gate 	uchar_t			passin_byte;
3257c478bd9Sstevel@tonic-gate 
326*54dcb32cSToomas Soome 	if (arg == (intptr_t)NULL) {
3277c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "LM75: ioctl: arg passed in to ioctl "
32819397407SSherry Moore 		    "= NULL\n"));
3297c478bd9Sstevel@tonic-gate 		err = EINVAL;
3307c478bd9Sstevel@tonic-gate 		return (err);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
3337c478bd9Sstevel@tonic-gate 	unitp = (struct lm75_unit *)
33419397407SSherry Moore 	    ddi_get_soft_state(lm75soft_statep, instance);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
3377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
3387c478bd9Sstevel@tonic-gate 		err = ENOMEM;
3397c478bd9Sstevel@tonic-gate 		return (err);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->lm75_mutex);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	switch (cmd) {
3457c478bd9Sstevel@tonic-gate 	case I2C_GET_TEMPERATURE:
3467c478bd9Sstevel@tonic-gate 		err = lm75_get16(arg, LM75_TEMPERATURE_REG, unitp, mode);
3477c478bd9Sstevel@tonic-gate 		break;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	case LM75_GET_HYST:
3507c478bd9Sstevel@tonic-gate 		err = lm75_get16(arg, LM75_HYST_REG, unitp, mode);
3517c478bd9Sstevel@tonic-gate 		break;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	case LM75_SET_HYST:
3547c478bd9Sstevel@tonic-gate 		err = lm75_set16(arg, LM75_HYST_REG, unitp, mode);
3557c478bd9Sstevel@tonic-gate 		break;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	case LM75_GET_OVERTEMP_SHUTDOWN:
3587c478bd9Sstevel@tonic-gate 		err = lm75_get16(arg, LM75_OVERTEMP_REG, unitp, mode);
3597c478bd9Sstevel@tonic-gate 		break;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	case LM75_SET_OVERTEMP_SHUTDOWN:
3627c478bd9Sstevel@tonic-gate 		err = lm75_set16(arg, LM75_OVERTEMP_REG, unitp, mode);
3637c478bd9Sstevel@tonic-gate 		break;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	case LM75_GET_CONFIG:
3667c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
36719397407SSherry Moore 		    1, 1, I2C_SLEEP);
3687c478bd9Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
3697c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
37019397407SSherry Moore 			    "i2c_tran_pointer not allocated\n",
37119397407SSherry Moore 			    unitp->lm75_name));
3727c478bd9Sstevel@tonic-gate 			err = ENOMEM;
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR_RD;
3767c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
3797c478bd9Sstevel@tonic-gate 		if (err) {
3807c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
38119397407SSherry Moore 			    "i2c_transfer routine\n",
38219397407SSherry Moore 			    unitp->lm75_name));
3837c478bd9Sstevel@tonic-gate 			i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)i2c_tran_pointer->i2c_rbuf,
38719397407SSherry Moore 		    (caddr_t)arg,
38819397407SSherry Moore 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
3897c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
39019397407SSherry Moore 			    "ddi_copyout routine\n",
39119397407SSherry Moore 			    unitp->lm75_name));
3927c478bd9Sstevel@tonic-gate 			err = EFAULT;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	case LM75_SET_CONFIG:
3987c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&passin_byte,
39919397407SSherry Moore 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
4007c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
40119397407SSherry Moore 			    "ddi_copyin routine\n",
40219397407SSherry Moore 			    unitp->lm75_name));
4037c478bd9Sstevel@tonic-gate 			err = EFAULT;
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
40719397407SSherry Moore 		    2, 0, I2C_SLEEP);
4087c478bd9Sstevel@tonic-gate 		if (i2c_tran_pointer == NULL) {
4097c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
41019397407SSherry Moore 			    "i2c_tran_pointer not allocated\n",
41119397407SSherry Moore 			    unitp->lm75_name));
4127c478bd9Sstevel@tonic-gate 			err = ENOMEM;
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_flags = I2C_WR;
4167c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
4177c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[1] = passin_byte;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
4207c478bd9Sstevel@tonic-gate 		i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	default:
4247c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd %x\n",
42519397407SSherry Moore 		    unitp->lm75_name, cmd));
4267c478bd9Sstevel@tonic-gate 		err = EINVAL;
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->lm75_mutex);
4307c478bd9Sstevel@tonic-gate 	return (err);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate static int
lm75_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4347c478bd9Sstevel@tonic-gate lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	switch (cmd) {
4377c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4387c478bd9Sstevel@tonic-gate 		return (lm75_do_attach(dip));
4397c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4407c478bd9Sstevel@tonic-gate 		return (lm75_do_resume());
4417c478bd9Sstevel@tonic-gate 	default:
4427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static int
lm75_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4477c478bd9Sstevel@tonic-gate lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	switch (cmd) {
4507c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4517c478bd9Sstevel@tonic-gate 		return (lm75_do_detach(dip));
4527c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4537c478bd9Sstevel@tonic-gate 		return (lm75_do_suspend());
4547c478bd9Sstevel@tonic-gate 	default:
4557c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate static int
lm75_do_attach(dev_info_t * dip)4607c478bd9Sstevel@tonic-gate lm75_do_attach(dev_info_t *dip)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	struct lm75_unit *unitp;
4637c478bd9Sstevel@tonic-gate 	int instance;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(lm75soft_statep, instance) != 0) {
4687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
46919397407SSherry Moore 		    ddi_get_name(dip), instance);
4707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
4767c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
4777c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	(void) snprintf(unitp->lm75_name, sizeof (unitp->lm75_name),
48119397407SSherry Moore 	    "%s%d", ddi_node_name(dip), instance);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "lm75", S_IFCHR, instance,
484*54dcb32cSToomas Soome 	    "ddi_i2c:temperature_sensor", 0) == DDI_FAILURE) {
4857c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
48619397407SSherry Moore 		    "%s\n", unitp->lm75_name, "lm75");
4877c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lm75soft_statep, instance);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->lm75_hdl) != I2C_SUCCESS) {
4937c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4947c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(lm75soft_statep, instance);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	mutex_init(&unitp->lm75_mutex, NULL, MUTEX_DRIVER, NULL);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate static int
lm75_do_resume(void)5057c478bd9Sstevel@tonic-gate lm75_do_resume(void)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	return (ret);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static int
lm75_do_suspend()5137c478bd9Sstevel@tonic-gate lm75_do_suspend()
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (ret);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static int
lm75_do_detach(dev_info_t * dip)5217c478bd9Sstevel@tonic-gate lm75_do_detach(dev_info_t *dip)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	struct lm75_unit *unitp;
5247c478bd9Sstevel@tonic-gate 	int instance;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(lm75soft_statep, instance);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
5317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
5327c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	i2c_client_unregister(unitp->lm75_hdl);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	mutex_destroy(&unitp->lm75_mutex);
5407c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(lm75soft_statep, instance);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5437c478bd9Sstevel@tonic-gate }
544