10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23946Smathue * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * The max1617 I2C is a temp acquisition device. As implemented on some
290Sstevel@tonic-gate * processor modules, it contains both a local and a remote temp. The
300Sstevel@tonic-gate * local temp measures the ambient (room) temperature, while the remote
310Sstevel@tonic-gate * sensor is connected to the processor die. There are ioctl's for retrieving
320Sstevel@tonic-gate * temperatures, and setting temperature alarm ranges.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/open.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/conf.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/note.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
470Sstevel@tonic-gate #include <sys/i2c/clients/i2c_client.h>
480Sstevel@tonic-gate #include <sys/i2c/clients/max1617.h>
490Sstevel@tonic-gate #include <sys/i2c/clients/max1617_impl.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * cb ops (only need ioctl)
530Sstevel@tonic-gate */
540Sstevel@tonic-gate static int max1617_open(dev_t *, int, int, cred_t *);
550Sstevel@tonic-gate static int max1617_close(dev_t, int, int, cred_t *);
560Sstevel@tonic-gate static int max1617_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
570Sstevel@tonic-gate
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate * dev ops
600Sstevel@tonic-gate */
610Sstevel@tonic-gate static int max1617_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
620Sstevel@tonic-gate void **result);
630Sstevel@tonic-gate static int max1617_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
640Sstevel@tonic-gate static int max1617_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
650Sstevel@tonic-gate
660Sstevel@tonic-gate static struct cb_ops max1617_cbops = {
670Sstevel@tonic-gate max1617_open, /* open */
680Sstevel@tonic-gate max1617_close, /* close */
690Sstevel@tonic-gate nodev, /* strategy */
700Sstevel@tonic-gate nodev, /* print */
710Sstevel@tonic-gate nodev, /* dump */
720Sstevel@tonic-gate nodev, /* read */
730Sstevel@tonic-gate nodev, /* write */
740Sstevel@tonic-gate max1617_ioctl, /* ioctl */
750Sstevel@tonic-gate nodev, /* devmap */
760Sstevel@tonic-gate nodev, /* mmap */
770Sstevel@tonic-gate nodev, /* segmap */
780Sstevel@tonic-gate nochpoll, /* poll */
790Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
800Sstevel@tonic-gate NULL, /* streamtab */
810Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */
820Sstevel@tonic-gate CB_REV, /* rev */
830Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
840Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
850Sstevel@tonic-gate };
860Sstevel@tonic-gate
870Sstevel@tonic-gate static struct dev_ops max1617_ops = {
880Sstevel@tonic-gate DEVO_REV,
890Sstevel@tonic-gate 0,
900Sstevel@tonic-gate max1617_info,
910Sstevel@tonic-gate nulldev,
920Sstevel@tonic-gate nulldev,
930Sstevel@tonic-gate max1617_attach,
940Sstevel@tonic-gate max1617_detach,
950Sstevel@tonic-gate nodev,
960Sstevel@tonic-gate &max1617_cbops,
97*7656SSherry.Moore@Sun.COM NULL,
98*7656SSherry.Moore@Sun.COM NULL,
99*7656SSherry.Moore@Sun.COM ddi_quiesce_not_supported, /* devo_quiesce */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate static struct modldrv max1617_modldrv = {
1030Sstevel@tonic-gate &mod_driverops, /* type of module - driver */
104*7656SSherry.Moore@Sun.COM "max1617 device driver",
1050Sstevel@tonic-gate &max1617_ops,
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static struct modlinkage max1617_modlinkage = {
1090Sstevel@tonic-gate MODREV_1,
1100Sstevel@tonic-gate &max1617_modldrv,
1110Sstevel@tonic-gate 0
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static int max1617_debug = 0;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate static void *max1617_soft_statep;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate int
_init(void)1190Sstevel@tonic-gate _init(void)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate int error;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate error = mod_install(&max1617_modlinkage);
1240Sstevel@tonic-gate if (error == 0) {
1250Sstevel@tonic-gate (void) ddi_soft_state_init(&max1617_soft_statep,
126*7656SSherry.Moore@Sun.COM sizeof (struct max1617_unit), 1);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate return (error);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate int
_fini(void)1330Sstevel@tonic-gate _fini(void)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate int error;
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate error = mod_remove(&max1617_modlinkage);
1380Sstevel@tonic-gate if (error == 0) {
1390Sstevel@tonic-gate ddi_soft_state_fini(&max1617_soft_statep);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate return (error);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1460Sstevel@tonic-gate _info(struct modinfo *modinfop)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate return (mod_info(&max1617_modlinkage, modinfop));
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /* ARGSUSED */
1520Sstevel@tonic-gate static int
max1617_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1530Sstevel@tonic-gate max1617_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate dev_t dev;
1560Sstevel@tonic-gate int instance;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (infocmd == DDI_INFO_DEVT2INSTANCE) {
1590Sstevel@tonic-gate dev = (dev_t)arg;
1600Sstevel@tonic-gate instance = MAX1617_MINOR_TO_INST(getminor(dev));
1610Sstevel@tonic-gate *result = (void *)(uintptr_t)instance;
1620Sstevel@tonic-gate return (DDI_SUCCESS);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate return (DDI_FAILURE);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate static int
max1617_do_attach(dev_info_t * dip)1680Sstevel@tonic-gate max1617_do_attach(dev_info_t *dip)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate struct max1617_unit *unitp;
1710Sstevel@tonic-gate int instance;
1720Sstevel@tonic-gate char minor_name[MAXNAMELEN];
1730Sstevel@tonic-gate minor_t minor_number;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate instance = ddi_get_instance(dip);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (ddi_soft_state_zalloc(max1617_soft_statep, instance) != 0) {
1780Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: failed to zalloc softstate",
1790Sstevel@tonic-gate ddi_get_name(dip), instance);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate return (DDI_FAILURE);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate unitp = ddi_get_soft_state(max1617_soft_statep, instance);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate (void) snprintf(unitp->max1617_name, sizeof (unitp->max1617_name),
187946Smathue "%s%d", ddi_node_name(dip), instance);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate (void) sprintf(minor_name, "die_temp");
1900Sstevel@tonic-gate minor_number = MAX1617_INST_TO_MINOR(instance) |
1910Sstevel@tonic-gate MAX1617_FCN_TO_MINOR(MAX1617_CPU_TEMP);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate if (ddi_create_minor_node(dip, minor_name, S_IFCHR,
1940Sstevel@tonic-gate minor_number, MAX1617_NODE_TYPE, NULL) == DDI_FAILURE) {
1950Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for minor "
1960Sstevel@tonic-gate " name '%s'", unitp->max1617_name, minor_name);
197*7656SSherry.Moore@Sun.COM ddi_soft_state_free(max1617_soft_statep, instance);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate return (DDI_FAILURE);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate (void) sprintf(minor_name, "amb_temp");
2030Sstevel@tonic-gate minor_number = MAX1617_INST_TO_MINOR(instance) |
2040Sstevel@tonic-gate MAX1617_FCN_TO_MINOR(MAX1617_AMB_TEMP);
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (ddi_create_minor_node(dip, minor_name, S_IFCHR,
2070Sstevel@tonic-gate minor_number, MAX1617_NODE_TYPE, NULL) == DDI_FAILURE) {
2080Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for %s",
2090Sstevel@tonic-gate unitp->max1617_name, minor_name);
2100Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
2110Sstevel@tonic-gate ddi_soft_state_free(max1617_soft_statep, instance);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate return (DDI_FAILURE);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->max1617_hdl) != I2C_SUCCESS) {
2170Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
2180Sstevel@tonic-gate ddi_soft_state_free(max1617_soft_statep, instance);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate return (DDI_FAILURE);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate mutex_init(&unitp->max1617_mutex, NULL, MUTEX_DRIVER, NULL);
2240Sstevel@tonic-gate cv_init(&unitp->max1617_cv, NULL, CV_DRIVER, NULL);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate return (DDI_SUCCESS);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate static int
max1617_do_resume(dev_info_t * dip)2300Sstevel@tonic-gate max1617_do_resume(dev_info_t *dip)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate int ret = DDI_SUCCESS;
2330Sstevel@tonic-gate int instance = ddi_get_instance(dip);
2340Sstevel@tonic-gate i2c_transfer_t *i2ctp;
2350Sstevel@tonic-gate struct max1617_unit *unitp;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if ((unitp = ddi_get_soft_state(max1617_soft_statep, instance)) ==
2380Sstevel@tonic-gate NULL) {
2390Sstevel@tonic-gate return (DDI_FAILURE);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl,
2430Sstevel@tonic-gate &i2ctp, 2, 0, I2C_SLEEP);
2440Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
2450Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONFIG_WR_REG;
2490Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_config;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2520Sstevel@tonic-gate ret = DDI_FAILURE;
2530Sstevel@tonic-gate goto done;
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONV_RATE_WR_REG;
2570Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_conv_rate;
2580Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2590Sstevel@tonic-gate ret = DDI_FAILURE;
2600Sstevel@tonic-gate goto done;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_LOCALTEMP_HIGH_WR_REG;
2640Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_lcl_hlimit;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2670Sstevel@tonic-gate ret = DDI_FAILURE;
2680Sstevel@tonic-gate goto done;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_REMOTETEMP_HIGH_WR_REG;
2720Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_remote_hlimit;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2750Sstevel@tonic-gate ret = DDI_FAILURE;
2760Sstevel@tonic-gate goto done;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_LOCALTEMP_LOW_REG;
2800Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_lcl_llimit;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2830Sstevel@tonic-gate ret = DDI_FAILURE;
2840Sstevel@tonic-gate goto done;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_REMOTETEMP_LOW_REG;
2880Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = unitp->max1617_cpr_state.max1617_remote_llimit;
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
2910Sstevel@tonic-gate ret = DDI_FAILURE;
2920Sstevel@tonic-gate goto done;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate done:
2960Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
2970Sstevel@tonic-gate unitp->max1617_flags = 0;
2980Sstevel@tonic-gate cv_signal(&unitp->max1617_cv);
2990Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
3020Sstevel@tonic-gate return (ret);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate static int
max1617_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3060Sstevel@tonic-gate max1617_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate switch (cmd) {
3090Sstevel@tonic-gate case DDI_ATTACH:
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate return (max1617_do_attach(dip));
3120Sstevel@tonic-gate case DDI_RESUME:
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate return (max1617_do_resume(dip));
3150Sstevel@tonic-gate default:
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate return (DDI_FAILURE);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate static int
max1617_do_detach(dev_info_t * dip)3220Sstevel@tonic-gate max1617_do_detach(dev_info_t *dip)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate struct max1617_unit *unitp;
3250Sstevel@tonic-gate int instance;
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate instance = ddi_get_instance(dip);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate unitp = ddi_get_soft_state(max1617_soft_statep, instance);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (unitp == NULL) {
3320Sstevel@tonic-gate return (DDI_FAILURE);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate i2c_client_unregister(unitp->max1617_hdl);
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate mutex_destroy(&unitp->max1617_mutex);
3400Sstevel@tonic-gate cv_destroy(&unitp->max1617_cv);
3410Sstevel@tonic-gate ddi_soft_state_free(max1617_soft_statep, instance);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate return (DDI_SUCCESS);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate static int
max1617_do_suspend(dev_info_t * dip)3470Sstevel@tonic-gate max1617_do_suspend(dev_info_t *dip)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate int ret = DDI_SUCCESS;
3500Sstevel@tonic-gate int instance = ddi_get_instance(dip);
3510Sstevel@tonic-gate i2c_transfer_t *i2ctp;
3520Sstevel@tonic-gate struct max1617_unit *unitp;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if ((unitp = ddi_get_soft_state(max1617_soft_statep, instance)) ==
3550Sstevel@tonic-gate NULL) {
3560Sstevel@tonic-gate return (DDI_FAILURE);
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl,
3600Sstevel@tonic-gate &i2ctp, 1, 1, I2C_SLEEP);
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate * Block new transactions during CPR
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
3670Sstevel@tonic-gate while (unitp->max1617_flags == MAX1617_BUSY) {
3680Sstevel@tonic-gate cv_wait(&unitp->max1617_cv, &unitp->max1617_mutex);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate unitp->max1617_flags = MAX1617_BUSY;
3710Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
3740Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
3750Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONFIG_REG;
3760Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
3770Sstevel@tonic-gate ret = DDI_FAILURE;
3780Sstevel@tonic-gate goto done;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_config = i2ctp->i2c_rbuf[0];
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONV_RATE_REG;
3830Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
3840Sstevel@tonic-gate ret = DDI_FAILURE;
3850Sstevel@tonic-gate goto done;
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_conv_rate = i2ctp->i2c_rbuf[0];
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_LOCALTEMP_HIGH_REG;
3900Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
3910Sstevel@tonic-gate ret = DDI_FAILURE;
3920Sstevel@tonic-gate goto done;
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_lcl_hlimit = i2ctp->i2c_rbuf[0];
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_REMOTETEMP_HIGH_REG;
3970Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
3980Sstevel@tonic-gate ret = DDI_FAILURE;
3990Sstevel@tonic-gate goto done;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_remote_hlimit = i2ctp->i2c_rbuf[0];
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_LOCALTEMP_LOW_REG;
4040Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
4050Sstevel@tonic-gate ret = DDI_FAILURE;
4060Sstevel@tonic-gate goto done;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_lcl_llimit = i2ctp->i2c_rbuf[0];
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_REMOTETEMP_LOW_REG;
4110Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
4120Sstevel@tonic-gate ret = DDI_FAILURE;
4130Sstevel@tonic-gate goto done;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate unitp->max1617_cpr_state.max1617_remote_llimit = i2ctp->i2c_rbuf[0];
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate done:
4180Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate if (ret == DDI_FAILURE) {
4210Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
4220Sstevel@tonic-gate unitp->max1617_flags = 0;
4230Sstevel@tonic-gate cv_broadcast(&unitp->max1617_cv);
4240Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate return (ret);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate static int
max1617_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4300Sstevel@tonic-gate max1617_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate switch (cmd) {
4330Sstevel@tonic-gate case DDI_DETACH:
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate return (max1617_do_detach(dip));
4360Sstevel@tonic-gate case DDI_SUSPEND:
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate return (max1617_do_suspend(dip));
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate default:
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate return (DDI_FAILURE);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate static int
max1617_open(dev_t * devp,int flags,int otyp,cred_t * credp)4470Sstevel@tonic-gate max1617_open(dev_t *devp, int flags, int otyp, cred_t *credp)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate _NOTE(ARGUNUSED(credp))
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate struct max1617_unit *unitp;
4520Sstevel@tonic-gate int instance;
4530Sstevel@tonic-gate int err = 0;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate instance = MAX1617_MINOR_TO_INST(getminor(*devp));
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate if (instance < 0) {
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate return (ENXIO);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate unitp = (struct max1617_unit *)
463*7656SSherry.Moore@Sun.COM ddi_get_soft_state(max1617_soft_statep, instance);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate if (unitp == NULL) {
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate return (ENXIO);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (otyp != OTYP_CHR) {
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate return (EINVAL);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate if (flags & FEXCL) {
4780Sstevel@tonic-gate if (unitp->max1617_oflag != 0) {
4790Sstevel@tonic-gate err = EBUSY;
4800Sstevel@tonic-gate } else {
4810Sstevel@tonic-gate unitp->max1617_oflag = FEXCL;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate } else {
4840Sstevel@tonic-gate if (unitp->max1617_oflag == FEXCL) {
4850Sstevel@tonic-gate err = EBUSY;
4860Sstevel@tonic-gate } else {
487946Smathue unitp->max1617_oflag = (uint16_t)FOPEN;
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate done:
4920Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate return (err);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate static int
max1617_close(dev_t dev,int flags,int otyp,cred_t * credp)4980Sstevel@tonic-gate max1617_close(dev_t dev, int flags, int otyp, cred_t *credp)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp))
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate struct max1617_unit *unitp;
5030Sstevel@tonic-gate int instance = MAX1617_MINOR_TO_INST(getminor(dev));
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate if (instance < 0) {
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate return (ENXIO);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate unitp = (struct max1617_unit *)
511*7656SSherry.Moore@Sun.COM ddi_get_soft_state(max1617_soft_statep, instance);
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if (unitp == NULL) {
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate return (ENXIO);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate unitp->max1617_oflag = 0;
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate return (DDI_SUCCESS);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate int
set_temp_limit(struct max1617_unit * unitp,uchar_t device_reg,caddr_t arg,int mode)5280Sstevel@tonic-gate set_temp_limit(struct max1617_unit *unitp,
5290Sstevel@tonic-gate uchar_t device_reg,
5300Sstevel@tonic-gate caddr_t arg,
5310Sstevel@tonic-gate int mode)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate int err = 0;
5340Sstevel@tonic-gate i2c_transfer_t *i2ctp;
5350Sstevel@tonic-gate int16_t temp;
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp, 2, 0, I2C_SLEEP);
5380Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
5390Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
5400Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = device_reg;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate if (ddi_copyin(arg, (caddr_t)&temp, sizeof (int16_t), mode) !=
5430Sstevel@tonic-gate DDI_SUCCESS) {
5440Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate return (EFAULT);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate i2ctp->i2c_wbuf[1] = (int8_t)temp;
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
5520Sstevel@tonic-gate err = EIO;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate return (err);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate int
get_temp_limit(struct max1617_unit * unitp,uchar_t reg,caddr_t arg,int mode)5610Sstevel@tonic-gate get_temp_limit(struct max1617_unit *unitp,
5620Sstevel@tonic-gate uchar_t reg,
5630Sstevel@tonic-gate caddr_t arg,
5640Sstevel@tonic-gate int mode)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate int err = 0;
5670Sstevel@tonic-gate i2c_transfer_t *i2ctp;
5680Sstevel@tonic-gate int16_t temp16;
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp, 1, 1, I2C_SLEEP);
5710Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
5720Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
5730Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = reg;
5740Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) == I2C_SUCCESS) {
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * This double cast is required so that the sign is preserved
5770Sstevel@tonic-gate * when expanding the 8 bit value to 16.
5780Sstevel@tonic-gate */
5790Sstevel@tonic-gate temp16 = (int16_t)((int8_t)i2ctp->i2c_rbuf[0]);
5800Sstevel@tonic-gate if (ddi_copyout((caddr_t)&temp16, arg, sizeof (int16_t),
5810Sstevel@tonic-gate mode) != DDI_SUCCESS) {
5820Sstevel@tonic-gate err = EFAULT;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate } else {
5850Sstevel@tonic-gate err = EIO;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate return (err);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate static int
max1617_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)5930Sstevel@tonic-gate max1617_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
5940Sstevel@tonic-gate cred_t *credp, int *rvalp)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate _NOTE(ARGUNUSED(credp, rvalp))
5970Sstevel@tonic-gate struct max1617_unit *unitp;
5980Sstevel@tonic-gate int err = 0;
5990Sstevel@tonic-gate i2c_transfer_t *i2ctp;
6000Sstevel@tonic-gate int fcn = MAX1617_MINOR_TO_FCN(getminor(dev));
6010Sstevel@tonic-gate int instance = MAX1617_MINOR_TO_INST(getminor(dev));
6020Sstevel@tonic-gate uchar_t reg;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate unitp = (struct max1617_unit *)
605*7656SSherry.Moore@Sun.COM ddi_get_soft_state(max1617_soft_statep, instance);
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate if (max1617_debug) {
6080Sstevel@tonic-gate printf("max1617_ioctl: fcn=%d instance=%d\n", fcn, instance);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /*
6120Sstevel@tonic-gate * Serialize here, in order to block transacations during CPR.
6130Sstevel@tonic-gate * This is not a bottle neck since i2c_transfer would serialize
6140Sstevel@tonic-gate * anyway.
6150Sstevel@tonic-gate */
6160Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
6170Sstevel@tonic-gate while (unitp->max1617_flags == MAX1617_BUSY) {
6180Sstevel@tonic-gate if (cv_wait_sig(&unitp->max1617_cv,
6190Sstevel@tonic-gate &unitp->max1617_mutex) <= 0) {
6200Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
6210Sstevel@tonic-gate return (EINTR);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate unitp->max1617_flags = MAX1617_BUSY;
6250Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate switch (cmd) {
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * I2C_GET_TEMPERATURE reads a temperature from the device and
6310Sstevel@tonic-gate * copies a single byte representing the celcius temp
6320Sstevel@tonic-gate * to user space.
6330Sstevel@tonic-gate */
6340Sstevel@tonic-gate case I2C_GET_TEMPERATURE:
6350Sstevel@tonic-gate switch (fcn) {
6360Sstevel@tonic-gate case MAX1617_AMB_TEMP:
6370Sstevel@tonic-gate reg = MAX1617_LOCAL_TEMP_REG;
6380Sstevel@tonic-gate break;
6390Sstevel@tonic-gate case MAX1617_CPU_TEMP:
6400Sstevel@tonic-gate reg = MAX1617_REMOTE_TEMP_REG;
6410Sstevel@tonic-gate break;
6420Sstevel@tonic-gate default:
6430Sstevel@tonic-gate err = EINVAL;
6440Sstevel@tonic-gate goto done;
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp,
6480Sstevel@tonic-gate 1, 1, I2C_SLEEP);
6490Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
6500Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
6510Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = reg;
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) == I2C_SUCCESS) {
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate * This double cast is needed so that the sign bit
6570Sstevel@tonic-gate * is preserved when casting from unsigned char to
6580Sstevel@tonic-gate * signed 16 bit value.
6590Sstevel@tonic-gate */
6600Sstevel@tonic-gate int16_t temp = (int16_t)((int8_t)i2ctp->i2c_rbuf[0]);
6610Sstevel@tonic-gate if (ddi_copyout((caddr_t)&temp, (caddr_t)arg,
6620Sstevel@tonic-gate sizeof (int16_t), mode) != DDI_SUCCESS) {
6630Sstevel@tonic-gate err = EFAULT;
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate } else {
6660Sstevel@tonic-gate err = EIO;
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
6690Sstevel@tonic-gate break;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate case MAX1617_GET_STATUS:
6720Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp,
6730Sstevel@tonic-gate 1, 1, I2C_SLEEP);
6740Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
6750Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
6760Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_STATUS_REG;
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) == I2C_SUCCESS) {
6790Sstevel@tonic-gate if (ddi_copyout((caddr_t)i2ctp->i2c_rbuf, (caddr_t)arg,
6800Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) {
6810Sstevel@tonic-gate err = EFAULT;
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate } else {
6840Sstevel@tonic-gate err = EIO;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
6870Sstevel@tonic-gate break;
6880Sstevel@tonic-gate case MAX1617_GET_CONFIG:
6890Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp, 1, 1,
6900Sstevel@tonic-gate I2C_SLEEP);
6910Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
6920Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
6930Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONFIG_REG;
6940Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) == I2C_SUCCESS) {
6950Sstevel@tonic-gate if (ddi_copyout((caddr_t)i2ctp->i2c_rbuf, (caddr_t)arg,
6960Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) {
6970Sstevel@tonic-gate err = EFAULT;
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate } else {
7000Sstevel@tonic-gate err = EIO;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
7030Sstevel@tonic-gate break;
7040Sstevel@tonic-gate case MAX1617_GET_CONV_RATE:
7050Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp,
7060Sstevel@tonic-gate 1, 1, I2C_SLEEP);
7070Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
7080Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
7090Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONV_RATE_REG;
7100Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) == I2C_SUCCESS) {
7110Sstevel@tonic-gate if (ddi_copyout((caddr_t)i2ctp->i2c_rbuf, (caddr_t)arg,
7120Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) {
7130Sstevel@tonic-gate err = EFAULT;
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate } else {
7160Sstevel@tonic-gate err = EIO;
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
7190Sstevel@tonic-gate break;
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate case MAX1617_GET_HIGH_LIMIT:
7220Sstevel@tonic-gate switch (fcn) {
7230Sstevel@tonic-gate case MAX1617_AMB_TEMP:
7240Sstevel@tonic-gate err = get_temp_limit(unitp, MAX1617_LOCALTEMP_HIGH_REG,
7250Sstevel@tonic-gate (caddr_t)arg, mode);
7260Sstevel@tonic-gate break;
7270Sstevel@tonic-gate case MAX1617_CPU_TEMP:
7280Sstevel@tonic-gate err = get_temp_limit(unitp, MAX1617_REMOTETEMP_HIGH_REG,
7290Sstevel@tonic-gate (caddr_t)arg, mode);
7300Sstevel@tonic-gate break;
7310Sstevel@tonic-gate default:
7320Sstevel@tonic-gate err = EINVAL;
7330Sstevel@tonic-gate break;
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate break;
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate case MAX1617_GET_LOW_LIMIT:
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate switch (fcn) {
7400Sstevel@tonic-gate case MAX1617_AMB_TEMP:
7410Sstevel@tonic-gate err = get_temp_limit(unitp, MAX1617_LOCALTEMP_LOW_REG,
7420Sstevel@tonic-gate (caddr_t)arg, mode);
7430Sstevel@tonic-gate break;
7440Sstevel@tonic-gate case MAX1617_CPU_TEMP:
7450Sstevel@tonic-gate err = get_temp_limit(unitp, MAX1617_REMOTETEMP_LOW_REG,
7460Sstevel@tonic-gate (caddr_t)arg, mode);
7470Sstevel@tonic-gate break;
7480Sstevel@tonic-gate default:
7490Sstevel@tonic-gate err = EINVAL;
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate break;
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate case MAX1617_SET_CONV_RATE:
7540Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp,
7550Sstevel@tonic-gate 2, 0, I2C_SLEEP);
7560Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
7570Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
7580Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONV_RATE_WR_REG;
7590Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&i2ctp->i2c_wbuf[1],
7600Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) {
7610Sstevel@tonic-gate err = EFAULT;
7620Sstevel@tonic-gate break;
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
7650Sstevel@tonic-gate err = EIO;
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
7680Sstevel@tonic-gate break;
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate case MAX1617_SET_CONFIG:
7710Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp,
7720Sstevel@tonic-gate 2, 0, I2C_SLEEP);
7730Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
7740Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
7750Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_CONFIG_WR_REG;
7760Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&i2ctp->i2c_wbuf[1],
7770Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) {
7780Sstevel@tonic-gate err = EFAULT;
7790Sstevel@tonic-gate break;
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
7820Sstevel@tonic-gate err = EIO;
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
7860Sstevel@tonic-gate break;
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate case MAX1617_SET_HIGH_LIMIT:
7890Sstevel@tonic-gate switch (fcn) {
7900Sstevel@tonic-gate case MAX1617_AMB_TEMP:
7910Sstevel@tonic-gate err = set_temp_limit(unitp,
7920Sstevel@tonic-gate MAX1617_LOCALTEMP_HIGH_WR_REG, (caddr_t)arg, mode);
7930Sstevel@tonic-gate break;
7940Sstevel@tonic-gate case MAX1617_CPU_TEMP:
7950Sstevel@tonic-gate err = set_temp_limit(unitp,
7960Sstevel@tonic-gate MAX1617_REMOTETEMP_HIGH_WR_REG, (caddr_t)arg, mode);
7970Sstevel@tonic-gate break;
7980Sstevel@tonic-gate default:
7990Sstevel@tonic-gate err = EINVAL;
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate break;
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate case MAX1617_SET_LOW_LIMIT:
8040Sstevel@tonic-gate switch (fcn) {
8050Sstevel@tonic-gate case MAX1617_AMB_TEMP:
8060Sstevel@tonic-gate err = set_temp_limit(unitp,
8070Sstevel@tonic-gate MAX1617_LOCALTEMP_LOW_WR_REG, (caddr_t)arg, mode);
8080Sstevel@tonic-gate break;
8090Sstevel@tonic-gate case MAX1617_CPU_TEMP:
8100Sstevel@tonic-gate err = set_temp_limit(unitp,
8110Sstevel@tonic-gate MAX1617_REMOTETEMP_LOW_WR_REG, (caddr_t)arg, mode);
8120Sstevel@tonic-gate break;
8130Sstevel@tonic-gate default:
8140Sstevel@tonic-gate err = EINVAL;
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate break;
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate case MAX1617_ONE_SHOT_CMD:
8190Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->max1617_hdl, &i2ctp, 1, 0,
8200Sstevel@tonic-gate I2C_SLEEP);
8210Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
8220Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
8230Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = MAX1617_ONE_SHOT_CMD_REG;
8240Sstevel@tonic-gate if (i2c_transfer(unitp->max1617_hdl, i2ctp) != I2C_SUCCESS) {
8250Sstevel@tonic-gate err = EIO;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate i2c_transfer_free(unitp->max1617_hdl, i2ctp);
8290Sstevel@tonic-gate break;
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate default:
8320Sstevel@tonic-gate err = EINVAL;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate done:
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate mutex_enter(&unitp->max1617_mutex);
8380Sstevel@tonic-gate unitp->max1617_flags = 0;
8390Sstevel@tonic-gate cv_signal(&unitp->max1617_cv);
8400Sstevel@tonic-gate mutex_exit(&unitp->max1617_mutex);
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate return (err);
8430Sstevel@tonic-gate }
844