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
_init(void)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
_fini(void)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
_info(struct modinfo * modinfop)138 _info(struct modinfo *modinfop)
139 {
140 return (mod_info(&lm75_modlinkage, modinfop));
141 }
142
143 static int
lm75_open(dev_t * devp,int flags,int otyp,cred_t * credp)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
lm75_close(dev_t dev,int flags,int otyp,cred_t * credp)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
lm75_get16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)220 lm75_get16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode)
221 {
222 i2c_transfer_t *i2c_tran_pointer;
223 int err = DDI_SUCCESS;
224 int16_t temp16;
225 int8_t holder;
226
227 (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
228 1, 2, I2C_SLEEP);
229 if (i2c_tran_pointer == NULL) {
230 D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
231 "i2c_tran_pointer not allocated\n", 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", unitp->lm75_name));
242 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
243 return (err);
244 }
245
246 D1CMN_ERR((CE_NOTE, "%s: rbuf[0] = %x rbuf[1] = %x\n",
247 unitp->lm75_name, i2c_tran_pointer->i2c_rbuf[0],
248 i2c_tran_pointer->i2c_rbuf[0]));
249 temp16 = i2c_tran_pointer->i2c_rbuf[0];
250 temp16 = (temp16 << 1);
251 temp16 = (temp16 | ((i2c_tran_pointer->i2c_rbuf[1] & 0x80) >> 7));
252
253
254 if (temp16 & LM75_COMP_MASK) {
255 holder = (temp16 & LM75_COMP_MASK_UPPER);
256 holder = -holder;
257 holder = holder/2;
258 temp16 = 0 - holder;
259 } else {
260 temp16 = temp16 / 2;
261 }
262 if (ddi_copyout((caddr_t)&temp16, (caddr_t)arg,
263 sizeof (int16_t), mode) != DDI_SUCCESS) {
264 D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
265 "ddi_copyout routine\n", unitp->lm75_name));
266 err = EFAULT;
267 }
268 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
269 return (err);
270 }
271
272 static int
lm75_set16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)273 lm75_set16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode)
274 {
275 i2c_transfer_t *i2c_tran_pointer;
276 int err = DDI_SUCCESS;
277 int16_t temp16;
278
279 if (ddi_copyin((caddr_t)arg, (caddr_t)&temp16,
280 sizeof (int16_t), mode) != DDI_SUCCESS) {
281 D2CMN_ERR((CE_WARN, "%s: Failed in LM74_SET_HYST "
282 "ddi_copyin routine\n", unitp->lm75_name));
283 return (EFAULT);
284 }
285
286 (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
287 3, 0, I2C_SLEEP);
288 if (i2c_tran_pointer == NULL) {
289 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_HYST "
290 "i2c_tran_pointer not allocated\n", unitp->lm75_name));
291 return (ENOMEM);
292 }
293
294 /* BEGIN CSTYLED */
295 /*
296 * The temperature is 16bits where the top 9 are a twos-complement
297 * word with the the least significant bit used to indicate 0.5C
298 *
299 * |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
300 * |-----------------------------------------------|
301 * |+-| Temperature | Unused |
302 */
303 /* END CSTYLED */
304 i2c_tran_pointer->i2c_flags = I2C_WR;
305 i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
306 i2c_tran_pointer->i2c_wbuf[1] = ((temp16 >> 1) & 0xff);
307 i2c_tran_pointer->i2c_wbuf[2] = ((temp16 & 0x1) << 7);
308
309 err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
310 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
311 return (err);
312 }
313
314 static int
lm75_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)315 lm75_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
316 int *rvalp)
317 {
318 _NOTE(ARGUNUSED(credp, rvalp))
319
320 struct lm75_unit *unitp;
321 int instance;
322 int err = 0;
323 i2c_transfer_t *i2c_tran_pointer;
324 uchar_t passin_byte;
325
326 if (arg == (intptr_t)NULL) {
327 D2CMN_ERR((CE_WARN, "LM75: ioctl: arg passed in to ioctl "
328 "= NULL\n"));
329 err = EINVAL;
330 return (err);
331 }
332 instance = getminor(dev);
333 unitp = (struct lm75_unit *)
334 ddi_get_soft_state(lm75soft_statep, instance);
335
336 if (unitp == NULL) {
337 cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
338 err = ENOMEM;
339 return (err);
340 }
341
342 mutex_enter(&unitp->lm75_mutex);
343
344 switch (cmd) {
345 case I2C_GET_TEMPERATURE:
346 err = lm75_get16(arg, LM75_TEMPERATURE_REG, unitp, mode);
347 break;
348
349 case LM75_GET_HYST:
350 err = lm75_get16(arg, LM75_HYST_REG, unitp, mode);
351 break;
352
353 case LM75_SET_HYST:
354 err = lm75_set16(arg, LM75_HYST_REG, unitp, mode);
355 break;
356
357 case LM75_GET_OVERTEMP_SHUTDOWN:
358 err = lm75_get16(arg, LM75_OVERTEMP_REG, unitp, mode);
359 break;
360
361 case LM75_SET_OVERTEMP_SHUTDOWN:
362 err = lm75_set16(arg, LM75_OVERTEMP_REG, unitp, mode);
363 break;
364
365 case LM75_GET_CONFIG:
366 (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
367 1, 1, I2C_SLEEP);
368 if (i2c_tran_pointer == NULL) {
369 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
370 "i2c_tran_pointer not allocated\n",
371 unitp->lm75_name));
372 err = ENOMEM;
373 break;
374 }
375 i2c_tran_pointer->i2c_flags = I2C_WR_RD;
376 i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
377
378 err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
379 if (err) {
380 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
381 "i2c_transfer routine\n",
382 unitp->lm75_name));
383 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
384 break;
385 }
386 if (ddi_copyout((caddr_t)i2c_tran_pointer->i2c_rbuf,
387 (caddr_t)arg,
388 sizeof (uint8_t), mode) != DDI_SUCCESS) {
389 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
390 "ddi_copyout routine\n",
391 unitp->lm75_name));
392 err = EFAULT;
393 }
394 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
395 break;
396
397 case LM75_SET_CONFIG:
398 if (ddi_copyin((caddr_t)arg, (caddr_t)&passin_byte,
399 sizeof (uint8_t), mode) != DDI_SUCCESS) {
400 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
401 "ddi_copyin routine\n",
402 unitp->lm75_name));
403 err = EFAULT;
404 break;
405 }
406 (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
407 2, 0, I2C_SLEEP);
408 if (i2c_tran_pointer == NULL) {
409 D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
410 "i2c_tran_pointer not allocated\n",
411 unitp->lm75_name));
412 err = ENOMEM;
413 break;
414 }
415 i2c_tran_pointer->i2c_flags = I2C_WR;
416 i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
417 i2c_tran_pointer->i2c_wbuf[1] = passin_byte;
418
419 err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
420 i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
421 break;
422
423 default:
424 D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd %x\n",
425 unitp->lm75_name, cmd));
426 err = EINVAL;
427 }
428
429 mutex_exit(&unitp->lm75_mutex);
430 return (err);
431 }
432
433 static int
lm75_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)434 lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
435 {
436 switch (cmd) {
437 case DDI_ATTACH:
438 return (lm75_do_attach(dip));
439 case DDI_RESUME:
440 return (lm75_do_resume());
441 default:
442 return (DDI_FAILURE);
443 }
444 }
445
446 static int
lm75_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)447 lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
448 {
449 switch (cmd) {
450 case DDI_DETACH:
451 return (lm75_do_detach(dip));
452 case DDI_SUSPEND:
453 return (lm75_do_suspend());
454 default:
455 return (DDI_FAILURE);
456 }
457 }
458
459 static int
lm75_do_attach(dev_info_t * dip)460 lm75_do_attach(dev_info_t *dip)
461 {
462 struct lm75_unit *unitp;
463 int instance;
464
465 instance = ddi_get_instance(dip);
466
467 if (ddi_soft_state_zalloc(lm75soft_statep, instance) != 0) {
468 cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
469 ddi_get_name(dip), instance);
470 return (DDI_FAILURE);
471 }
472
473 unitp = ddi_get_soft_state(lm75soft_statep, instance);
474
475 if (unitp == NULL) {
476 cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
477 return (ENOMEM);
478 }
479
480 (void) snprintf(unitp->lm75_name, sizeof (unitp->lm75_name),
481 "%s%d", ddi_node_name(dip), instance);
482
483 if (ddi_create_minor_node(dip, "lm75", S_IFCHR, instance,
484 "ddi_i2c:temperature_sensor", 0) == DDI_FAILURE) {
485 cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
486 "%s\n", unitp->lm75_name, "lm75");
487 ddi_soft_state_free(lm75soft_statep, instance);
488
489 return (DDI_FAILURE);
490 }
491
492 if (i2c_client_register(dip, &unitp->lm75_hdl) != I2C_SUCCESS) {
493 ddi_remove_minor_node(dip, NULL);
494 ddi_soft_state_free(lm75soft_statep, instance);
495
496 return (DDI_FAILURE);
497 }
498
499 mutex_init(&unitp->lm75_mutex, NULL, MUTEX_DRIVER, NULL);
500
501 return (DDI_SUCCESS);
502 }
503
504 static int
lm75_do_resume(void)505 lm75_do_resume(void)
506 {
507 int ret = DDI_SUCCESS;
508
509 return (ret);
510 }
511
512 static int
lm75_do_suspend()513 lm75_do_suspend()
514 {
515 int ret = DDI_SUCCESS;
516
517 return (ret);
518 }
519
520 static int
lm75_do_detach(dev_info_t * dip)521 lm75_do_detach(dev_info_t *dip)
522 {
523 struct lm75_unit *unitp;
524 int instance;
525
526 instance = ddi_get_instance(dip);
527
528 unitp = ddi_get_soft_state(lm75soft_statep, instance);
529
530 if (unitp == NULL) {
531 cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
532 return (ENOMEM);
533 }
534
535 i2c_client_unregister(unitp->lm75_hdl);
536
537 ddi_remove_minor_node(dip, NULL);
538
539 mutex_destroy(&unitp->lm75_mutex);
540 ddi_soft_state_free(lm75soft_statep, instance);
541
542 return (DDI_SUCCESS);
543 }
544