1 /* $NetBSD: i2c.c,v 1.36 2011/10/03 23:53:04 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.36 2011/10/03 23:53:04 jmcneill Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/device.h> 44 #include <sys/event.h> 45 #include <sys/conf.h> 46 #include <sys/malloc.h> 47 #include <sys/kmem.h> 48 #include <sys/kthread.h> 49 #include <sys/proc.h> 50 #include <sys/kernel.h> 51 #include <sys/fcntl.h> 52 #include <sys/module.h> 53 54 #include <dev/i2c/i2cvar.h> 55 56 #include "locators.h" 57 58 #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */ 59 60 struct iic_softc { 61 i2c_tag_t sc_tag; 62 int sc_type; 63 device_t sc_devices[I2C_MAX_ADDR + 1]; 64 }; 65 66 static dev_type_open(iic_open); 67 static dev_type_close(iic_close); 68 static dev_type_ioctl(iic_ioctl); 69 70 const struct cdevsw iic_cdevsw = { 71 iic_open, iic_close, noread, nowrite, iic_ioctl, 72 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER 73 }; 74 75 extern struct cfdriver iic_cd; 76 77 static void iic_smbus_intr_thread(void *); 78 static void iic_fill_compat(struct i2c_attach_args*, const char*, 79 size_t, char **); 80 81 static int 82 iic_print_direct(void *aux, const char *pnp) 83 { 84 struct i2c_attach_args *ia = aux; 85 86 if (pnp != NULL) 87 aprint_normal("%s at %s addr 0x%02x", ia->ia_name, pnp, 88 ia->ia_addr); 89 else 90 aprint_normal(" addr 0x%02x", ia->ia_addr); 91 92 return UNCONF; 93 } 94 95 static int 96 iic_print(void *aux, const char *pnp) 97 { 98 struct i2c_attach_args *ia = aux; 99 100 if (ia->ia_addr != (i2c_addr_t)-1) 101 aprint_normal(" addr 0x%x", ia->ia_addr); 102 103 return UNCONF; 104 } 105 106 static int 107 iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 108 { 109 struct iic_softc *sc = device_private(parent); 110 struct i2c_attach_args ia; 111 112 ia.ia_tag = sc->sc_tag; 113 ia.ia_addr = cf->cf_loc[IICCF_ADDR]; 114 ia.ia_size = cf->cf_loc[IICCF_SIZE]; 115 ia.ia_type = sc->sc_type; 116 117 ia.ia_name = NULL; 118 ia.ia_ncompat = 0; 119 ia.ia_compat = NULL; 120 121 if (config_match(parent, cf, &ia) > 0) { 122 if (ia.ia_addr != (i2c_addr_t)-1 && 123 ia.ia_addr <= I2C_MAX_ADDR && 124 !sc->sc_devices[ia.ia_addr]) 125 sc->sc_devices[ia.ia_addr] = 126 config_attach(parent, cf, &ia, iic_print); 127 } 128 return 0; 129 } 130 131 static void 132 iic_child_detach(device_t parent, device_t child) 133 { 134 struct iic_softc *sc = device_private(parent); 135 int i; 136 137 for (i = 0; i <= I2C_MAX_ADDR; i++) 138 if (sc->sc_devices[i] == child) { 139 sc->sc_devices[i] = NULL; 140 break; 141 } 142 } 143 144 static int 145 iic_rescan(device_t self, const char *ifattr, const int *locators) 146 { 147 config_search_ia(iic_search, self, ifattr, NULL); 148 return 0; 149 } 150 151 static int 152 iic_match(device_t parent, cfdata_t cf, void *aux) 153 { 154 155 return 1; 156 } 157 158 static void 159 iic_attach(device_t parent, device_t self, void *aux) 160 { 161 struct iic_softc *sc = device_private(self); 162 struct i2cbus_attach_args *iba = aux; 163 prop_array_t child_devices; 164 char *buf; 165 i2c_tag_t ic; 166 int rv; 167 168 aprint_naive("\n"); 169 aprint_normal(": I2C bus\n"); 170 171 sc->sc_tag = iba->iba_tag; 172 sc->sc_type = iba->iba_type; 173 ic = sc->sc_tag; 174 ic->ic_devname = device_xname(self); 175 176 LIST_INIT(&(sc->sc_tag->ic_list)); 177 LIST_INIT(&(sc->sc_tag->ic_proc_list)); 178 179 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 180 iic_smbus_intr_thread, ic, &ic->ic_intr_thread, 181 "%s", ic->ic_devname); 182 if (rv) 183 aprint_error_dev(self, "unable to create intr thread\n"); 184 185 if (!pmf_device_register(self, NULL, NULL)) 186 aprint_error_dev(self, "couldn't establish power handler\n"); 187 188 child_devices = prop_dictionary_get(device_properties(parent), 189 "i2c-child-devices"); 190 if (child_devices) { 191 unsigned int i, count; 192 prop_dictionary_t dev; 193 prop_data_t cdata; 194 uint32_t addr, size; 195 uint64_t cookie; 196 const char *name; 197 struct i2c_attach_args ia; 198 int loc[2]; 199 200 memset(loc, 0, sizeof loc); 201 count = prop_array_count(child_devices); 202 for (i = 0; i < count; i++) { 203 dev = prop_array_get(child_devices, i); 204 if (!dev) continue; 205 if (!prop_dictionary_get_cstring_nocopy( 206 dev, "name", &name)) 207 continue; 208 if (!prop_dictionary_get_uint32(dev, "addr", &addr)) 209 continue; 210 if (!prop_dictionary_get_uint64(dev, "cookie", &cookie)) 211 cookie = 0; 212 loc[0] = addr; 213 if (prop_dictionary_get_uint32(dev, "size", &size)) 214 loc[1] = size; 215 else 216 loc[1] = -1; 217 218 memset(&ia, 0, sizeof ia); 219 ia.ia_addr = addr; 220 ia.ia_type = sc->sc_type; 221 ia.ia_tag = ic; 222 ia.ia_name = name; 223 ia.ia_cookie = cookie; 224 225 buf = NULL; 226 cdata = prop_dictionary_get(dev, "compatible"); 227 if (cdata) 228 iic_fill_compat(&ia, 229 prop_data_data_nocopy(cdata), 230 prop_data_size(cdata), &buf); 231 232 if (addr > I2C_MAX_ADDR) { 233 aprint_error_dev(self, 234 "WARNING: ignoring bad device address " 235 "@ 0x%02x\n", addr); 236 } else if (ia.ia_addr == addr) { 237 aprint_error_dev(self, 238 "WARNING: ignoring duplicate device " 239 "@ 0x%02x\n", addr); 240 } else if (sc->sc_devices[addr] == NULL) { 241 sc->sc_devices[addr] = 242 config_found_sm_loc(self, "iic", loc, &ia, 243 iic_print_direct, NULL); 244 } 245 246 if (ia.ia_compat) 247 free(ia.ia_compat, M_TEMP); 248 if (buf) 249 free(buf, M_TEMP); 250 } 251 } else { 252 /* 253 * Attach all i2c devices described in the kernel 254 * configuration file. 255 */ 256 iic_rescan(self, "iic", NULL); 257 } 258 } 259 260 static int 261 iic_detach(device_t self, int flags) 262 { 263 struct iic_softc *sc = device_private(self); 264 i2c_tag_t ic = sc->sc_tag; 265 int i, error; 266 void *hdl; 267 268 for (i = 0; i <= I2C_MAX_ADDR; i++) { 269 if (sc->sc_devices[i]) { 270 error = config_detach(sc->sc_devices[i], flags); 271 if (error) 272 return error; 273 } 274 } 275 276 if (ic->ic_running) { 277 ic->ic_running = 0; 278 wakeup(ic); 279 kthread_join(ic->ic_intr_thread); 280 } 281 282 if (!LIST_EMPTY(&ic->ic_list)) { 283 device_printf(self, "WARNING: intr handler list not empty\n"); 284 while (!LIST_EMPTY(&ic->ic_list)) { 285 hdl = LIST_FIRST(&ic->ic_list); 286 iic_smbus_intr_disestablish(ic, hdl); 287 } 288 } 289 if (!LIST_EMPTY(&ic->ic_proc_list)) { 290 device_printf(self, "WARNING: proc handler list not empty\n"); 291 while (!LIST_EMPTY(&ic->ic_proc_list)) { 292 hdl = LIST_FIRST(&ic->ic_proc_list); 293 iic_smbus_intr_disestablish_proc(ic, hdl); 294 } 295 } 296 297 pmf_device_deregister(self); 298 299 return 0; 300 } 301 302 static void 303 iic_smbus_intr_thread(void *aux) 304 { 305 i2c_tag_t ic; 306 struct ic_intr_list *il; 307 int rv; 308 309 ic = (i2c_tag_t)aux; 310 ic->ic_running = 1; 311 ic->ic_pending = 0; 312 313 while (ic->ic_running) { 314 if (ic->ic_pending == 0) 315 rv = tsleep(ic, PZERO, "iicintr", hz); 316 if (ic->ic_pending > 0) { 317 LIST_FOREACH(il, &(ic->ic_proc_list), il_next) { 318 (*il->il_intr)(il->il_intrarg); 319 } 320 ic->ic_pending--; 321 } 322 } 323 324 kthread_exit(0); 325 } 326 327 void * 328 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 329 { 330 struct ic_intr_list *il; 331 332 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 333 if (il == NULL) 334 return NULL; 335 336 il->il_intr = intr; 337 il->il_intrarg = intrarg; 338 339 LIST_INSERT_HEAD(&(ic->ic_list), il, il_next); 340 341 return il; 342 } 343 344 void 345 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl) 346 { 347 struct ic_intr_list *il; 348 349 il = (struct ic_intr_list *)hdl; 350 351 LIST_REMOVE(il, il_next); 352 free(il, M_DEVBUF); 353 354 return; 355 } 356 357 void * 358 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 359 { 360 struct ic_intr_list *il; 361 362 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 363 if (il == NULL) 364 return NULL; 365 366 il->il_intr = intr; 367 il->il_intrarg = intrarg; 368 369 LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next); 370 371 return il; 372 } 373 374 void 375 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl) 376 { 377 struct ic_intr_list *il; 378 379 il = (struct ic_intr_list *)hdl; 380 381 LIST_REMOVE(il, il_next); 382 free(il, M_DEVBUF); 383 384 return; 385 } 386 387 int 388 iic_smbus_intr(i2c_tag_t ic) 389 { 390 struct ic_intr_list *il; 391 392 LIST_FOREACH(il, &(ic->ic_list), il_next) { 393 (*il->il_intr)(il->il_intrarg); 394 } 395 396 ic->ic_pending++; 397 wakeup(ic); 398 399 return 1; 400 } 401 402 static void 403 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len, 404 char **buffer) 405 { 406 int count, i; 407 const char *c, *start, **ptr; 408 409 *buffer = NULL; 410 for (i = count = 0, c = compat; i < len; i++, c++) 411 if (*c == 0) 412 count++; 413 count += 2; 414 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK); 415 if (!ptr) return; 416 417 for (i = count = 0, start = c = compat; i < len; i++, c++) { 418 if (*c == 0) { 419 ptr[count++] = start; 420 start = c+1; 421 } 422 } 423 if (start < compat+len) { 424 /* last string not 0 terminated */ 425 size_t l = c-start; 426 *buffer = malloc(l+1, M_TEMP, M_WAITOK); 427 memcpy(*buffer, start, l); 428 (*buffer)[l] = 0; 429 ptr[count++] = *buffer; 430 } 431 ptr[count] = NULL; 432 433 ia->ia_compat = ptr; 434 ia->ia_ncompat = count; 435 } 436 437 int 438 iic_compat_match(struct i2c_attach_args *ia, const char ** compats) 439 { 440 int i; 441 442 for (; compats && *compats; compats++) { 443 for (i = 0; i < ia->ia_ncompat; i++) { 444 if (strcmp(*compats, ia->ia_compat[i]) == 0) 445 return 1; 446 } 447 } 448 return 0; 449 } 450 451 static int 452 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 453 { 454 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 455 456 if (sc == NULL) 457 return ENXIO; 458 459 return 0; 460 } 461 462 static int 463 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 464 { 465 return 0; 466 } 467 468 static int 469 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 470 { 471 i2c_tag_t ic = sc->sc_tag; 472 uint8_t buf[I2C_EXEC_MAX_BUFLEN]; 473 void *cmd = NULL; 474 int error; 475 476 /* Validate parameters */ 477 if (iie->iie_addr > I2C_MAX_ADDR) 478 return EINVAL; 479 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 480 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 481 return EINVAL; 482 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 483 return EINVAL; 484 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 485 return EINVAL; 486 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 487 return EBADF; 488 489 #if 0 490 /* Disallow userspace access to devices that have drivers attached. */ 491 if (sc->sc_devices[iie->iie_addr] != NULL) 492 return EBUSY; 493 #endif 494 495 if (iie->iie_cmd != NULL) { 496 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 497 if (cmd == NULL) 498 return ENOMEM; 499 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 500 if (error) { 501 kmem_free(cmd, iie->iie_cmdlen); 502 return error; 503 } 504 } 505 506 iic_acquire_bus(ic, 0); 507 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 508 buf, iie->iie_buflen, 0); 509 iic_release_bus(ic, 0); 510 511 /* 512 * Some drivers return error codes on failure, and others return -1. 513 */ 514 if (error < 0) 515 error = EIO; 516 517 if (cmd) 518 kmem_free(cmd, iie->iie_cmdlen); 519 520 if (error) 521 return error; 522 523 if (iie->iie_buf) 524 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 525 526 return error; 527 } 528 529 static int 530 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 531 { 532 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 533 534 if (sc == NULL) 535 return ENXIO; 536 537 switch (cmd) { 538 case I2C_IOCTL_EXEC: 539 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 540 default: 541 return ENODEV; 542 } 543 } 544 545 546 CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc), 547 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach); 548 549 MODULE(MODULE_CLASS_DRIVER, iic, NULL); 550 551 #ifdef _MODULE 552 #include "ioconf.c" 553 #endif 554 555 static int 556 iic_modcmd(modcmd_t cmd, void *opaque) 557 { 558 int error; 559 560 error = 0; 561 switch (cmd) { 562 case MODULE_CMD_INIT: 563 #ifdef _MODULE 564 error = config_init_component(cfdriver_ioconf_iic, 565 cfattach_ioconf_iic, cfdata_ioconf_iic); 566 if (error) 567 aprint_error("%s: unable to init component\n", 568 iic_cd.cd_name); 569 #endif 570 break; 571 case MODULE_CMD_FINI: 572 #ifdef _MODULE 573 config_fini_component(cfdriver_ioconf_iic, 574 cfattach_ioconf_iic, cfdata_ioconf_iic); 575 #endif 576 break; 577 default: 578 error = ENOTTY; 579 } 580 return error; 581 } 582