1 /* $NetBSD: i2c.c,v 1.40 2013/08/07 19:38:45 soren 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.40 2013/08/07 19:38:45 soren 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_size = cf->cf_loc[IICCF_SIZE]; 114 ia.ia_type = sc->sc_type; 115 116 ia.ia_name = NULL; 117 ia.ia_ncompat = 0; 118 ia.ia_compat = NULL; 119 120 for (ia.ia_addr = 0; ia.ia_addr <= I2C_MAX_ADDR; ia.ia_addr++) { 121 if (sc->sc_devices[ia.ia_addr] != NULL) 122 continue; 123 124 if (cf->cf_loc[IICCF_ADDR] != -1 && 125 cf->cf_loc[IICCF_ADDR] != ia.ia_addr) 126 continue; 127 128 if (config_match(parent, cf, &ia) > 0) 129 sc->sc_devices[ia.ia_addr] = 130 config_attach(parent, cf, &ia, iic_print); 131 } 132 133 return 0; 134 } 135 136 static void 137 iic_child_detach(device_t parent, device_t child) 138 { 139 struct iic_softc *sc = device_private(parent); 140 int i; 141 142 for (i = 0; i <= I2C_MAX_ADDR; i++) 143 if (sc->sc_devices[i] == child) { 144 sc->sc_devices[i] = NULL; 145 break; 146 } 147 } 148 149 static int 150 iic_rescan(device_t self, const char *ifattr, const int *locators) 151 { 152 config_search_ia(iic_search, self, ifattr, NULL); 153 return 0; 154 } 155 156 static int 157 iic_match(device_t parent, cfdata_t cf, void *aux) 158 { 159 160 return 1; 161 } 162 163 static void 164 iic_attach(device_t parent, device_t self, void *aux) 165 { 166 struct iic_softc *sc = device_private(self); 167 struct i2cbus_attach_args *iba = aux; 168 prop_array_t child_devices; 169 char *buf; 170 i2c_tag_t ic; 171 int rv; 172 173 aprint_naive("\n"); 174 aprint_normal(": I2C bus\n"); 175 176 sc->sc_tag = iba->iba_tag; 177 sc->sc_type = iba->iba_type; 178 ic = sc->sc_tag; 179 ic->ic_devname = device_xname(self); 180 181 LIST_INIT(&(sc->sc_tag->ic_list)); 182 LIST_INIT(&(sc->sc_tag->ic_proc_list)); 183 184 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 185 iic_smbus_intr_thread, ic, &ic->ic_intr_thread, 186 "%s", ic->ic_devname); 187 if (rv) 188 aprint_error_dev(self, "unable to create intr thread\n"); 189 190 if (!pmf_device_register(self, NULL, NULL)) 191 aprint_error_dev(self, "couldn't establish power handler\n"); 192 193 child_devices = prop_dictionary_get(device_properties(parent), 194 "i2c-child-devices"); 195 if (child_devices) { 196 unsigned int i, count; 197 prop_dictionary_t dev; 198 prop_data_t cdata; 199 uint32_t addr, size; 200 uint64_t cookie; 201 const char *name; 202 struct i2c_attach_args ia; 203 int loc[2]; 204 205 memset(loc, 0, sizeof loc); 206 count = prop_array_count(child_devices); 207 for (i = 0; i < count; i++) { 208 dev = prop_array_get(child_devices, i); 209 if (!dev) continue; 210 if (!prop_dictionary_get_cstring_nocopy( 211 dev, "name", &name)) 212 continue; 213 if (!prop_dictionary_get_uint32(dev, "addr", &addr)) 214 continue; 215 if (!prop_dictionary_get_uint64(dev, "cookie", &cookie)) 216 cookie = 0; 217 loc[0] = addr; 218 if (prop_dictionary_get_uint32(dev, "size", &size)) 219 loc[1] = size; 220 else 221 loc[1] = -1; 222 223 memset(&ia, 0, sizeof ia); 224 ia.ia_addr = addr; 225 ia.ia_type = sc->sc_type; 226 ia.ia_tag = ic; 227 ia.ia_name = name; 228 ia.ia_cookie = cookie; 229 ia.ia_size = size; 230 231 buf = NULL; 232 cdata = prop_dictionary_get(dev, "compatible"); 233 if (cdata) 234 iic_fill_compat(&ia, 235 prop_data_data_nocopy(cdata), 236 prop_data_size(cdata), &buf); 237 238 if (addr > I2C_MAX_ADDR) { 239 aprint_error_dev(self, 240 "WARNING: ignoring bad device address " 241 "@ 0x%02x\n", addr); 242 } else if (sc->sc_devices[addr] == NULL) { 243 sc->sc_devices[addr] = 244 config_found_sm_loc(self, "iic", loc, &ia, 245 iic_print_direct, NULL); 246 } 247 248 if (ia.ia_compat) 249 free(ia.ia_compat, M_TEMP); 250 if (buf) 251 free(buf, M_TEMP); 252 } 253 } else { 254 /* 255 * Attach all i2c devices described in the kernel 256 * configuration file. 257 */ 258 iic_rescan(self, "iic", NULL); 259 } 260 } 261 262 static int 263 iic_detach(device_t self, int flags) 264 { 265 struct iic_softc *sc = device_private(self); 266 i2c_tag_t ic = sc->sc_tag; 267 int i, error; 268 void *hdl; 269 270 for (i = 0; i <= I2C_MAX_ADDR; i++) { 271 if (sc->sc_devices[i]) { 272 error = config_detach(sc->sc_devices[i], flags); 273 if (error) 274 return error; 275 } 276 } 277 278 if (ic->ic_running) { 279 ic->ic_running = 0; 280 wakeup(ic); 281 kthread_join(ic->ic_intr_thread); 282 } 283 284 if (!LIST_EMPTY(&ic->ic_list)) { 285 device_printf(self, "WARNING: intr handler list not empty\n"); 286 while (!LIST_EMPTY(&ic->ic_list)) { 287 hdl = LIST_FIRST(&ic->ic_list); 288 iic_smbus_intr_disestablish(ic, hdl); 289 } 290 } 291 if (!LIST_EMPTY(&ic->ic_proc_list)) { 292 device_printf(self, "WARNING: proc handler list not empty\n"); 293 while (!LIST_EMPTY(&ic->ic_proc_list)) { 294 hdl = LIST_FIRST(&ic->ic_proc_list); 295 iic_smbus_intr_disestablish_proc(ic, hdl); 296 } 297 } 298 299 pmf_device_deregister(self); 300 301 return 0; 302 } 303 304 static void 305 iic_smbus_intr_thread(void *aux) 306 { 307 i2c_tag_t ic; 308 struct ic_intr_list *il; 309 int rv; 310 311 ic = (i2c_tag_t)aux; 312 ic->ic_running = 1; 313 ic->ic_pending = 0; 314 315 while (ic->ic_running) { 316 if (ic->ic_pending == 0) 317 rv = tsleep(ic, PZERO, "iicintr", hz); 318 if (ic->ic_pending > 0) { 319 LIST_FOREACH(il, &(ic->ic_proc_list), il_next) { 320 (*il->il_intr)(il->il_intrarg); 321 } 322 ic->ic_pending--; 323 } 324 } 325 326 kthread_exit(0); 327 } 328 329 void * 330 iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 331 { 332 struct ic_intr_list *il; 333 334 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 335 if (il == NULL) 336 return NULL; 337 338 il->il_intr = intr; 339 il->il_intrarg = intrarg; 340 341 LIST_INSERT_HEAD(&(ic->ic_list), il, il_next); 342 343 return il; 344 } 345 346 void 347 iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl) 348 { 349 struct ic_intr_list *il; 350 351 il = (struct ic_intr_list *)hdl; 352 353 LIST_REMOVE(il, il_next); 354 free(il, M_DEVBUF); 355 356 return; 357 } 358 359 void * 360 iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg) 361 { 362 struct ic_intr_list *il; 363 364 il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK); 365 if (il == NULL) 366 return NULL; 367 368 il->il_intr = intr; 369 il->il_intrarg = intrarg; 370 371 LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next); 372 373 return il; 374 } 375 376 void 377 iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl) 378 { 379 struct ic_intr_list *il; 380 381 il = (struct ic_intr_list *)hdl; 382 383 LIST_REMOVE(il, il_next); 384 free(il, M_DEVBUF); 385 386 return; 387 } 388 389 int 390 iic_smbus_intr(i2c_tag_t ic) 391 { 392 struct ic_intr_list *il; 393 394 LIST_FOREACH(il, &(ic->ic_list), il_next) { 395 (*il->il_intr)(il->il_intrarg); 396 } 397 398 ic->ic_pending++; 399 wakeup(ic); 400 401 return 1; 402 } 403 404 static void 405 iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len, 406 char **buffer) 407 { 408 int count, i; 409 const char *c, *start, **ptr; 410 411 *buffer = NULL; 412 for (i = count = 0, c = compat; i < len; i++, c++) 413 if (*c == 0) 414 count++; 415 count += 2; 416 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK); 417 if (!ptr) return; 418 419 for (i = count = 0, start = c = compat; i < len; i++, c++) { 420 if (*c == 0) { 421 ptr[count++] = start; 422 start = c+1; 423 } 424 } 425 if (start < compat+len) { 426 /* last string not 0 terminated */ 427 size_t l = c-start; 428 *buffer = malloc(l+1, M_TEMP, M_WAITOK); 429 memcpy(*buffer, start, l); 430 (*buffer)[l] = 0; 431 ptr[count++] = *buffer; 432 } 433 ptr[count] = NULL; 434 435 ia->ia_compat = ptr; 436 ia->ia_ncompat = count; 437 } 438 439 int 440 iic_compat_match(struct i2c_attach_args *ia, const char ** compats) 441 { 442 int i; 443 444 for (; compats && *compats; compats++) { 445 for (i = 0; i < ia->ia_ncompat; i++) { 446 if (strcmp(*compats, ia->ia_compat[i]) == 0) 447 return 1; 448 } 449 } 450 return 0; 451 } 452 453 static int 454 iic_open(dev_t dev, int flag, int fmt, lwp_t *l) 455 { 456 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 457 458 if (sc == NULL) 459 return ENXIO; 460 461 return 0; 462 } 463 464 static int 465 iic_close(dev_t dev, int flag, int fmt, lwp_t *l) 466 { 467 return 0; 468 } 469 470 static int 471 iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag) 472 { 473 i2c_tag_t ic = sc->sc_tag; 474 uint8_t buf[I2C_EXEC_MAX_BUFLEN]; 475 void *cmd = NULL; 476 int error; 477 478 /* Validate parameters */ 479 if (iie->iie_addr > I2C_MAX_ADDR) 480 return EINVAL; 481 if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN || 482 iie->iie_buflen > I2C_EXEC_MAX_BUFLEN) 483 return EINVAL; 484 if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0) 485 return EINVAL; 486 if (iie->iie_buf != NULL && iie->iie_buflen == 0) 487 return EINVAL; 488 if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0) 489 return EBADF; 490 491 #if 0 492 /* Disallow userspace access to devices that have drivers attached. */ 493 if (sc->sc_devices[iie->iie_addr] != NULL) 494 return EBUSY; 495 #endif 496 497 if (iie->iie_cmd != NULL) { 498 cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP); 499 if (cmd == NULL) 500 return ENOMEM; 501 error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen); 502 if (error) { 503 kmem_free(cmd, iie->iie_cmdlen); 504 return error; 505 } 506 } 507 508 iic_acquire_bus(ic, 0); 509 error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen, 510 buf, iie->iie_buflen, 0); 511 iic_release_bus(ic, 0); 512 513 /* 514 * Some drivers return error codes on failure, and others return -1. 515 */ 516 if (error < 0) 517 error = EIO; 518 519 if (cmd) 520 kmem_free(cmd, iie->iie_cmdlen); 521 522 if (error) 523 return error; 524 525 if (iie->iie_buf) 526 error = copyout(buf, iie->iie_buf, iie->iie_buflen); 527 528 return error; 529 } 530 531 static int 532 iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 533 { 534 struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev)); 535 536 if (sc == NULL) 537 return ENXIO; 538 539 switch (cmd) { 540 case I2C_IOCTL_EXEC: 541 return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag); 542 default: 543 return ENODEV; 544 } 545 } 546 547 548 CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc), 549 iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach); 550 551 MODULE(MODULE_CLASS_DRIVER, iic, NULL); 552 553 #ifdef _MODULE 554 #include "ioconf.c" 555 #endif 556 557 static int 558 iic_modcmd(modcmd_t cmd, void *opaque) 559 { 560 int error; 561 562 error = 0; 563 switch (cmd) { 564 case MODULE_CMD_INIT: 565 #ifdef _MODULE 566 error = config_init_component(cfdriver_ioconf_iic, 567 cfattach_ioconf_iic, cfdata_ioconf_iic); 568 if (error) 569 aprint_error("%s: unable to init component\n", 570 iic_cd.cd_name); 571 #endif 572 break; 573 case MODULE_CMD_FINI: 574 #ifdef _MODULE 575 config_fini_component(cfdriver_ioconf_iic, 576 cfattach_ioconf_iic, cfdata_ioconf_iic); 577 #endif 578 break; 579 default: 580 error = ENOTTY; 581 } 582 return error; 583 } 584