gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* $OpenBSD: gpio.c,v 1.13 2014/07/12 18:48:17 tedu Exp $ */
  2. /*
  3. * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org>
  4. * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * General Purpose Input/Output framework.
  20. */
  21. #include <sys/param.h>
  22. #include <sys/systm.h>
  23. #include <sys/conf.h>
  24. #include <sys/device.h>
  25. #include <sys/fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/gpio.h>
  28. #include <sys/vnode.h>
  29. #include <sys/malloc.h>
  30. #include <sys/queue.h>
  31. #include <dev/gpio/gpiovar.h>
  32. struct gpio_softc {
  33. struct device sc_dev;
  34. gpio_chipset_tag_t sc_gc; /* GPIO controller */
  35. gpio_pin_t *sc_pins; /* pins array */
  36. int sc_npins; /* number of pins */
  37. int sc_opened;
  38. LIST_HEAD(, gpio_dev) sc_devs; /* devices */
  39. LIST_HEAD(, gpio_name) sc_names; /* named pins */
  40. };
  41. int gpio_match(struct device *, void *, void *);
  42. int gpio_submatch(struct device *, void *, void *);
  43. void gpio_attach(struct device *, struct device *, void *);
  44. int gpio_detach(struct device *, int);
  45. int gpio_search(struct device *, void *, void *);
  46. int gpio_print(void *, const char *);
  47. int gpio_pinbyname(struct gpio_softc *, char *gp_name);
  48. struct cfattach gpio_ca = {
  49. sizeof (struct gpio_softc),
  50. gpio_match,
  51. gpio_attach,
  52. gpio_detach
  53. };
  54. struct cfdriver gpio_cd = {
  55. NULL, "gpio", DV_DULL
  56. };
  57. int
  58. gpio_match(struct device *parent, void *match, void *aux)
  59. {
  60. struct cfdata *cf = match;
  61. struct gpiobus_attach_args *gba = aux;
  62. return (strcmp(gba->gba_name, cf->cf_driver->cd_name) == 0);
  63. }
  64. int
  65. gpio_submatch(struct device *parent, void *match, void *aux)
  66. {
  67. struct cfdata *cf = match;
  68. struct gpio_attach_args *ga = aux;
  69. if (strcmp(ga->ga_dvname, cf->cf_driver->cd_name) != 0)
  70. return (0);
  71. return ((*cf->cf_attach->ca_match)(parent, match, aux));
  72. }
  73. void
  74. gpio_attach(struct device *parent, struct device *self, void *aux)
  75. {
  76. struct gpio_softc *sc = (struct gpio_softc *)self;
  77. struct gpiobus_attach_args *gba = aux;
  78. sc->sc_gc = gba->gba_gc;
  79. sc->sc_pins = gba->gba_pins;
  80. sc->sc_npins = gba->gba_npins;
  81. printf(": %d pins\n", sc->sc_npins);
  82. /*
  83. * Attach all devices that can be connected to the GPIO pins
  84. * described in the kernel configuration file.
  85. */
  86. config_search(gpio_search, self, sc);
  87. }
  88. int
  89. gpio_detach(struct device *self, int flags)
  90. {
  91. int maj, mn;
  92. /* Locate the major number */
  93. for (maj = 0; maj < nchrdev; maj++)
  94. if (cdevsw[maj].d_open == gpioopen)
  95. break;
  96. /* Nuke the vnodes for any open instances (calls close) */
  97. mn = self->dv_unit;
  98. vdevgone(maj, mn, mn, VCHR);
  99. return (0);
  100. }
  101. int
  102. gpio_search(struct device *parent, void *arg, void *aux)
  103. {
  104. struct cfdata *cf = arg;
  105. struct gpio_attach_args ga;
  106. ga.ga_gpio = aux;
  107. ga.ga_offset = cf->cf_loc[0];
  108. ga.ga_mask = cf->cf_loc[1];
  109. ga.ga_flags = cf->cf_loc[2];
  110. if (cf->cf_attach->ca_match(parent, cf, &ga) > 0)
  111. config_attach(parent, cf, &ga, gpio_print);
  112. return (0);
  113. }
  114. int
  115. gpio_print(void *aux, const char *pnp)
  116. {
  117. struct gpio_attach_args *ga = aux;
  118. int i;
  119. printf(" pins");
  120. for (i = 0; i < 32; i++)
  121. if (ga->ga_mask & (1 << i))
  122. printf(" %d", ga->ga_offset + i);
  123. return (UNCONF);
  124. }
  125. int
  126. gpiobus_print(void *aux, const char *pnp)
  127. {
  128. struct gpiobus_attach_args *gba = aux;
  129. if (pnp != NULL)
  130. printf("%s at %s", gba->gba_name, pnp);
  131. return (UNCONF);
  132. }
  133. int
  134. gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map)
  135. {
  136. struct gpio_softc *sc = gpio;
  137. int npins, pin, i;
  138. npins = gpio_npins(mask);
  139. if (npins > sc->sc_npins)
  140. return (1);
  141. for (npins = 0, i = 0; i < 32; i++)
  142. if (mask & (1 << i)) {
  143. pin = offset + i;
  144. if (pin < 0 || pin >= sc->sc_npins)
  145. return (1);
  146. if (sc->sc_pins[pin].pin_mapped)
  147. return (1);
  148. sc->sc_pins[pin].pin_mapped = 1;
  149. map->pm_map[npins++] = pin;
  150. }
  151. map->pm_size = npins;
  152. return (0);
  153. }
  154. void
  155. gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
  156. {
  157. struct gpio_softc *sc = gpio;
  158. int pin, i;
  159. for (i = 0; i < map->pm_size; i++) {
  160. pin = map->pm_map[i];
  161. sc->sc_pins[pin].pin_mapped = 0;
  162. }
  163. }
  164. int
  165. gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
  166. {
  167. struct gpio_softc *sc = gpio;
  168. return (gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]));
  169. }
  170. void
  171. gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
  172. {
  173. struct gpio_softc *sc = gpio;
  174. return (gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value));
  175. }
  176. void
  177. gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
  178. {
  179. struct gpio_softc *sc = gpio;
  180. return (gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags));
  181. }
  182. int
  183. gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
  184. {
  185. struct gpio_softc *sc = gpio;
  186. return (sc->sc_pins[map->pm_map[pin]].pin_caps);
  187. }
  188. int
  189. gpio_npins(u_int32_t mask)
  190. {
  191. int npins, i;
  192. for (npins = 0, i = 0; i < 32; i++)
  193. if (mask & (1 << i))
  194. npins++;
  195. return (npins);
  196. }
  197. int
  198. gpioopen(dev_t dev, int flag, int mode, struct proc *p)
  199. {
  200. struct gpio_softc *sc;
  201. sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
  202. if (sc == NULL)
  203. return (ENXIO);
  204. if (sc->sc_opened)
  205. return (EBUSY);
  206. sc->sc_opened = 1;
  207. return (0);
  208. }
  209. int
  210. gpioclose(dev_t dev, int flag, int mode, struct proc *p)
  211. {
  212. struct gpio_softc *sc;
  213. sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
  214. sc->sc_opened = 0;
  215. return (0);
  216. }
  217. int
  218. gpio_pinbyname(struct gpio_softc *sc, char *gp_name)
  219. {
  220. struct gpio_name *nm;
  221. LIST_FOREACH(nm, &sc->sc_names, gp_next)
  222. if (!strcmp(nm->gp_name, gp_name))
  223. return (nm->gp_pin);
  224. return (-1);
  225. }
  226. int
  227. gpioioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
  228. {
  229. struct gpio_softc *sc;
  230. gpio_chipset_tag_t gc;
  231. struct gpio_info *info;
  232. struct gpio_pin_op *op;
  233. struct gpio_attach *attach;
  234. struct gpio_attach_args ga;
  235. struct gpio_dev *gdev;
  236. struct gpio_name *nm;
  237. struct gpio_pin_set *set;
  238. struct device *dv;
  239. int pin, value, flags, npins, found;
  240. sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
  241. gc = sc->sc_gc;
  242. switch (cmd) {
  243. case GPIOINFO:
  244. info = (struct gpio_info *)data;
  245. if (securelevel < 1)
  246. info->gpio_npins = sc->sc_npins;
  247. else {
  248. for (pin = npins = 0; pin < sc->sc_npins; pin++)
  249. if (sc->sc_pins[pin].pin_flags & GPIO_PIN_SET)
  250. ++npins;
  251. info->gpio_npins = npins;
  252. }
  253. break;
  254. case GPIOPINREAD:
  255. op = (struct gpio_pin_op *)data;
  256. if (op->gp_name[0] != '\0') {
  257. pin = gpio_pinbyname(sc, op->gp_name);
  258. if (pin == -1)
  259. return (EINVAL);
  260. } else
  261. pin = op->gp_pin;
  262. if (pin < 0 || pin >= sc->sc_npins)
  263. return (EINVAL);
  264. if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
  265. securelevel > 0)
  266. return (EPERM);
  267. /* return read value */
  268. op->gp_value = gpiobus_pin_read(gc, pin);
  269. break;
  270. case GPIOPINWRITE:
  271. if ((flag & FWRITE) == 0)
  272. return (EBADF);
  273. op = (struct gpio_pin_op *)data;
  274. if (op->gp_name[0] != '\0') {
  275. pin = gpio_pinbyname(sc, op->gp_name);
  276. if (pin == -1)
  277. return (EINVAL);
  278. } else
  279. pin = op->gp_pin;
  280. if (pin < 0 || pin >= sc->sc_npins)
  281. return (EINVAL);
  282. if (sc->sc_pins[pin].pin_mapped)
  283. return (EBUSY);
  284. if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
  285. securelevel > 0)
  286. return (EPERM);
  287. value = op->gp_value;
  288. if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
  289. return (EINVAL);
  290. gpiobus_pin_write(gc, pin, value);
  291. /* return old value */
  292. op->gp_value = sc->sc_pins[pin].pin_state;
  293. /* update current value */
  294. sc->sc_pins[pin].pin_state = value;
  295. break;
  296. case GPIOPINTOGGLE:
  297. if ((flag & FWRITE) == 0)
  298. return (EBADF);
  299. op = (struct gpio_pin_op *)data;
  300. if (op->gp_name[0] != '\0') {
  301. pin = gpio_pinbyname(sc, op->gp_name);
  302. if (pin == -1)
  303. return (EINVAL);
  304. } else
  305. pin = op->gp_pin;
  306. if (pin < 0 || pin >= sc->sc_npins)
  307. return (EINVAL);
  308. if (sc->sc_pins[pin].pin_mapped)
  309. return (EBUSY);
  310. if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET) &&
  311. securelevel > 0)
  312. return (EPERM);
  313. value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
  314. GPIO_PIN_HIGH : GPIO_PIN_LOW);
  315. gpiobus_pin_write(gc, pin, value);
  316. /* return old value */
  317. op->gp_value = sc->sc_pins[pin].pin_state;
  318. /* update current value */
  319. sc->sc_pins[pin].pin_state = value;
  320. break;
  321. case GPIOATTACH:
  322. if (securelevel > 0)
  323. return (EPERM);
  324. attach = (struct gpio_attach *)data;
  325. bzero(&ga, sizeof(ga));
  326. ga.ga_gpio = sc;
  327. ga.ga_dvname = attach->ga_dvname;
  328. ga.ga_offset = attach->ga_offset;
  329. ga.ga_mask = attach->ga_mask;
  330. ga.ga_flags = attach->ga_flags;
  331. dv = config_found_sm((struct device *)sc, &ga, gpiobus_print,
  332. gpio_submatch);
  333. if (dv != NULL) {
  334. gdev = malloc(sizeof(struct gpio_dev), M_DEVBUF,
  335. M_WAITOK);
  336. gdev->sc_dev = dv;
  337. LIST_INSERT_HEAD(&sc->sc_devs, gdev, sc_next);
  338. }
  339. break;
  340. case GPIODETACH:
  341. if (securelevel > 0)
  342. return (EPERM);
  343. attach = (struct gpio_attach *)data;
  344. LIST_FOREACH(gdev, &sc->sc_devs, sc_next) {
  345. if (strcmp(gdev->sc_dev->dv_xname, attach->ga_dvname)
  346. == 0) {
  347. if (config_detach(gdev->sc_dev, 0) == 0) {
  348. LIST_REMOVE(gdev, sc_next);
  349. free(gdev, M_DEVBUF, 0);
  350. }
  351. break;
  352. }
  353. }
  354. break;
  355. case GPIOPINSET:
  356. if (securelevel > 0)
  357. return (EPERM);
  358. set = (struct gpio_pin_set *)data;
  359. if (set->gp_name[0] != '\0') {
  360. pin = gpio_pinbyname(sc, set->gp_name);
  361. if (pin == -1)
  362. return (EINVAL);
  363. } else
  364. pin = set->gp_pin;
  365. if (pin < 0 || pin >= sc->sc_npins)
  366. return (EINVAL);
  367. flags = set->gp_flags;
  368. /* check that the controller supports all requested flags */
  369. if ((flags & sc->sc_pins[pin].pin_caps) != flags)
  370. return (ENODEV);
  371. flags = set->gp_flags | GPIO_PIN_SET;
  372. set->gp_caps = sc->sc_pins[pin].pin_caps;
  373. /* return old value */
  374. set->gp_flags = sc->sc_pins[pin].pin_flags;
  375. if (flags > 0) {
  376. gpiobus_pin_ctl(gc, pin, flags);
  377. /* update current value */
  378. sc->sc_pins[pin].pin_flags = flags;
  379. }
  380. /* rename pin or new pin? */
  381. if (set->gp_name2[0] != '\0') {
  382. found = 0;
  383. LIST_FOREACH(nm, &sc->sc_names, gp_next)
  384. if (nm->gp_pin == pin) {
  385. strlcpy(nm->gp_name, set->gp_name2,
  386. sizeof(nm->gp_name));
  387. found = 1;
  388. break;
  389. }
  390. if (!found) {
  391. nm = malloc(sizeof(struct gpio_name),
  392. M_DEVBUF, M_WAITOK);
  393. strlcpy(nm->gp_name, set->gp_name2,
  394. sizeof(nm->gp_name));
  395. nm->gp_pin = set->gp_pin;
  396. LIST_INSERT_HEAD(&sc->sc_names, nm, gp_next);
  397. }
  398. }
  399. break;
  400. case GPIOPINUNSET:
  401. if (securelevel > 0)
  402. return (EPERM);
  403. set = (struct gpio_pin_set *)data;
  404. if (set->gp_name[0] != '\0') {
  405. pin = gpio_pinbyname(sc, set->gp_name);
  406. if (pin == -1)
  407. return (EINVAL);
  408. } else
  409. pin = set->gp_pin;
  410. if (pin < 0 || pin >= sc->sc_npins)
  411. return (EINVAL);
  412. if (sc->sc_pins[pin].pin_mapped)
  413. return (EBUSY);
  414. if (!(sc->sc_pins[pin].pin_flags & GPIO_PIN_SET))
  415. return (EINVAL);
  416. LIST_FOREACH(nm, &sc->sc_names, gp_next) {
  417. if (nm->gp_pin == pin) {
  418. LIST_REMOVE(nm, gp_next);
  419. free(nm, M_DEVBUF, 0);
  420. break;
  421. }
  422. }
  423. sc->sc_pins[pin].pin_flags &= ~GPIO_PIN_SET;
  424. break;
  425. default:
  426. return (ENOTTY);
  427. }
  428. return (0);
  429. }