pinctrl-mcp23s08.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /* MCP23S08 SPI/I2C GPIO driver */
  2. #include <linux/kernel.h>
  3. #include <linux/device.h>
  4. #include <linux/mutex.h>
  5. #include <linux/module.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/i2c.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/spi/mcp23s08.h>
  10. #include <linux/slab.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/pinctrl/pinctrl.h>
  16. #include <linux/pinctrl/pinconf.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. /*
  19. * MCP types supported by driver
  20. */
  21. #define MCP_TYPE_S08 0
  22. #define MCP_TYPE_S17 1
  23. #define MCP_TYPE_008 2
  24. #define MCP_TYPE_017 3
  25. #define MCP_TYPE_S18 4
  26. #define MCP_TYPE_018 5
  27. #define MCP_MAX_DEV_PER_CS 8
  28. /* Registers are all 8 bits wide.
  29. *
  30. * The mcp23s17 has twice as many bits, and can be configured to work
  31. * with either 16 bit registers or with two adjacent 8 bit banks.
  32. */
  33. #define MCP_IODIR 0x00 /* init/reset: all ones */
  34. #define MCP_IPOL 0x01
  35. #define MCP_GPINTEN 0x02
  36. #define MCP_DEFVAL 0x03
  37. #define MCP_INTCON 0x04
  38. #define MCP_IOCON 0x05
  39. # define IOCON_MIRROR (1 << 6)
  40. # define IOCON_SEQOP (1 << 5)
  41. # define IOCON_HAEN (1 << 3)
  42. # define IOCON_ODR (1 << 2)
  43. # define IOCON_INTPOL (1 << 1)
  44. # define IOCON_INTCC (1)
  45. #define MCP_GPPU 0x06
  46. #define MCP_INTF 0x07
  47. #define MCP_INTCAP 0x08
  48. #define MCP_GPIO 0x09
  49. #define MCP_OLAT 0x0a
  50. struct mcp23s08;
  51. struct mcp23s08 {
  52. u8 addr;
  53. bool irq_active_high;
  54. bool reg_shift;
  55. u16 irq_rise;
  56. u16 irq_fall;
  57. int irq;
  58. bool irq_controller;
  59. int cached_gpio;
  60. /* lock protects regmap access with bypass/cache flags */
  61. struct mutex lock;
  62. struct gpio_chip chip;
  63. struct regmap *regmap;
  64. struct device *dev;
  65. struct pinctrl_dev *pctldev;
  66. struct pinctrl_desc pinctrl_desc;
  67. };
  68. static const struct reg_default mcp23x08_defaults[] = {
  69. {.reg = MCP_IODIR, .def = 0xff},
  70. {.reg = MCP_IPOL, .def = 0x00},
  71. {.reg = MCP_GPINTEN, .def = 0x00},
  72. {.reg = MCP_DEFVAL, .def = 0x00},
  73. {.reg = MCP_INTCON, .def = 0x00},
  74. {.reg = MCP_IOCON, .def = 0x00},
  75. {.reg = MCP_GPPU, .def = 0x00},
  76. {.reg = MCP_OLAT, .def = 0x00},
  77. };
  78. static const struct regmap_range mcp23x08_volatile_range = {
  79. .range_min = MCP_INTF,
  80. .range_max = MCP_GPIO,
  81. };
  82. static const struct regmap_access_table mcp23x08_volatile_table = {
  83. .yes_ranges = &mcp23x08_volatile_range,
  84. .n_yes_ranges = 1,
  85. };
  86. static const struct regmap_range mcp23x08_precious_range = {
  87. .range_min = MCP_GPIO,
  88. .range_max = MCP_GPIO,
  89. };
  90. static const struct regmap_access_table mcp23x08_precious_table = {
  91. .yes_ranges = &mcp23x08_precious_range,
  92. .n_yes_ranges = 1,
  93. };
  94. static const struct regmap_config mcp23x08_regmap = {
  95. .reg_bits = 8,
  96. .val_bits = 8,
  97. .reg_stride = 1,
  98. .volatile_table = &mcp23x08_volatile_table,
  99. .precious_table = &mcp23x08_precious_table,
  100. .reg_defaults = mcp23x08_defaults,
  101. .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
  102. .cache_type = REGCACHE_FLAT,
  103. .max_register = MCP_OLAT,
  104. };
  105. static const struct reg_default mcp23x16_defaults[] = {
  106. {.reg = MCP_IODIR << 1, .def = 0xffff},
  107. {.reg = MCP_IPOL << 1, .def = 0x0000},
  108. {.reg = MCP_GPINTEN << 1, .def = 0x0000},
  109. {.reg = MCP_DEFVAL << 1, .def = 0x0000},
  110. {.reg = MCP_INTCON << 1, .def = 0x0000},
  111. {.reg = MCP_IOCON << 1, .def = 0x0000},
  112. {.reg = MCP_GPPU << 1, .def = 0x0000},
  113. {.reg = MCP_OLAT << 1, .def = 0x0000},
  114. };
  115. static const struct regmap_range mcp23x16_volatile_range = {
  116. .range_min = MCP_INTF << 1,
  117. .range_max = MCP_GPIO << 1,
  118. };
  119. static const struct regmap_access_table mcp23x16_volatile_table = {
  120. .yes_ranges = &mcp23x16_volatile_range,
  121. .n_yes_ranges = 1,
  122. };
  123. static const struct regmap_range mcp23x16_precious_range = {
  124. .range_min = MCP_GPIO << 1,
  125. .range_max = MCP_GPIO << 1,
  126. };
  127. static const struct regmap_access_table mcp23x16_precious_table = {
  128. .yes_ranges = &mcp23x16_precious_range,
  129. .n_yes_ranges = 1,
  130. };
  131. static const struct regmap_config mcp23x17_regmap = {
  132. .reg_bits = 8,
  133. .val_bits = 16,
  134. .reg_stride = 2,
  135. .max_register = MCP_OLAT << 1,
  136. .volatile_table = &mcp23x16_volatile_table,
  137. .precious_table = &mcp23x16_precious_table,
  138. .reg_defaults = mcp23x16_defaults,
  139. .num_reg_defaults = ARRAY_SIZE(mcp23x16_defaults),
  140. .cache_type = REGCACHE_FLAT,
  141. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  142. };
  143. static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
  144. {
  145. return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
  146. }
  147. static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
  148. {
  149. return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
  150. }
  151. static int mcp_set_mask(struct mcp23s08 *mcp, unsigned int reg,
  152. unsigned int mask, bool enabled)
  153. {
  154. u16 val = enabled ? 0xffff : 0x0000;
  155. return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift,
  156. mask, val);
  157. }
  158. static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg,
  159. unsigned int pin, bool enabled)
  160. {
  161. u16 mask = BIT(pin);
  162. return mcp_set_mask(mcp, reg, mask, enabled);
  163. }
  164. static const struct pinctrl_pin_desc mcp23x08_pins[] = {
  165. PINCTRL_PIN(0, "gpio0"),
  166. PINCTRL_PIN(1, "gpio1"),
  167. PINCTRL_PIN(2, "gpio2"),
  168. PINCTRL_PIN(3, "gpio3"),
  169. PINCTRL_PIN(4, "gpio4"),
  170. PINCTRL_PIN(5, "gpio5"),
  171. PINCTRL_PIN(6, "gpio6"),
  172. PINCTRL_PIN(7, "gpio7"),
  173. };
  174. static const struct pinctrl_pin_desc mcp23x17_pins[] = {
  175. PINCTRL_PIN(0, "gpio0"),
  176. PINCTRL_PIN(1, "gpio1"),
  177. PINCTRL_PIN(2, "gpio2"),
  178. PINCTRL_PIN(3, "gpio3"),
  179. PINCTRL_PIN(4, "gpio4"),
  180. PINCTRL_PIN(5, "gpio5"),
  181. PINCTRL_PIN(6, "gpio6"),
  182. PINCTRL_PIN(7, "gpio7"),
  183. PINCTRL_PIN(8, "gpio8"),
  184. PINCTRL_PIN(9, "gpio9"),
  185. PINCTRL_PIN(10, "gpio10"),
  186. PINCTRL_PIN(11, "gpio11"),
  187. PINCTRL_PIN(12, "gpio12"),
  188. PINCTRL_PIN(13, "gpio13"),
  189. PINCTRL_PIN(14, "gpio14"),
  190. PINCTRL_PIN(15, "gpio15"),
  191. };
  192. static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  193. {
  194. return 0;
  195. }
  196. static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  197. unsigned int group)
  198. {
  199. return NULL;
  200. }
  201. static int mcp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  202. unsigned int group,
  203. const unsigned int **pins,
  204. unsigned int *num_pins)
  205. {
  206. return -ENOTSUPP;
  207. }
  208. static const struct pinctrl_ops mcp_pinctrl_ops = {
  209. .get_groups_count = mcp_pinctrl_get_groups_count,
  210. .get_group_name = mcp_pinctrl_get_group_name,
  211. .get_group_pins = mcp_pinctrl_get_group_pins,
  212. #ifdef CONFIG_OF
  213. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  214. .dt_free_map = pinconf_generic_dt_free_map,
  215. #endif
  216. };
  217. static int mcp_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  218. unsigned long *config)
  219. {
  220. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  221. enum pin_config_param param = pinconf_to_config_param(*config);
  222. unsigned int data, status;
  223. int ret;
  224. switch (param) {
  225. case PIN_CONFIG_BIAS_PULL_UP:
  226. ret = mcp_read(mcp, MCP_GPPU, &data);
  227. if (ret < 0)
  228. return ret;
  229. status = (data & BIT(pin)) ? 1 : 0;
  230. break;
  231. default:
  232. dev_err(mcp->dev, "Invalid config param %04x\n", param);
  233. return -ENOTSUPP;
  234. }
  235. *config = 0;
  236. return status ? 0 : -EINVAL;
  237. }
  238. static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  239. unsigned long *configs, unsigned int num_configs)
  240. {
  241. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  242. enum pin_config_param param;
  243. u32 arg;
  244. int ret = 0;
  245. int i;
  246. for (i = 0; i < num_configs; i++) {
  247. param = pinconf_to_config_param(configs[i]);
  248. arg = pinconf_to_config_argument(configs[i]);
  249. switch (param) {
  250. case PIN_CONFIG_BIAS_PULL_UP:
  251. ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
  252. break;
  253. default:
  254. dev_err(mcp->dev, "Invalid config param %04x\n", param);
  255. return -ENOTSUPP;
  256. }
  257. }
  258. return ret;
  259. }
  260. static const struct pinconf_ops mcp_pinconf_ops = {
  261. .pin_config_get = mcp_pinconf_get,
  262. .pin_config_set = mcp_pinconf_set,
  263. .is_generic = true,
  264. };
  265. /*----------------------------------------------------------------------*/
  266. #ifdef CONFIG_SPI_MASTER
  267. static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
  268. {
  269. struct mcp23s08 *mcp = context;
  270. struct spi_device *spi = to_spi_device(mcp->dev);
  271. struct spi_message m;
  272. struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
  273. { .tx_buf = data, .len = count, }, };
  274. spi_message_init(&m);
  275. spi_message_add_tail(&t[0], &m);
  276. spi_message_add_tail(&t[1], &m);
  277. return spi_sync(spi, &m);
  278. }
  279. static int mcp23sxx_spi_gather_write(void *context,
  280. const void *reg, size_t reg_size,
  281. const void *val, size_t val_size)
  282. {
  283. struct mcp23s08 *mcp = context;
  284. struct spi_device *spi = to_spi_device(mcp->dev);
  285. struct spi_message m;
  286. struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
  287. { .tx_buf = reg, .len = reg_size, },
  288. { .tx_buf = val, .len = val_size, }, };
  289. spi_message_init(&m);
  290. spi_message_add_tail(&t[0], &m);
  291. spi_message_add_tail(&t[1], &m);
  292. spi_message_add_tail(&t[2], &m);
  293. return spi_sync(spi, &m);
  294. }
  295. static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
  296. void *val, size_t val_size)
  297. {
  298. struct mcp23s08 *mcp = context;
  299. struct spi_device *spi = to_spi_device(mcp->dev);
  300. u8 tx[2];
  301. if (reg_size != 1)
  302. return -EINVAL;
  303. tx[0] = mcp->addr | 0x01;
  304. tx[1] = *((u8 *) reg);
  305. return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
  306. }
  307. static const struct regmap_bus mcp23sxx_spi_regmap = {
  308. .write = mcp23sxx_spi_write,
  309. .gather_write = mcp23sxx_spi_gather_write,
  310. .read = mcp23sxx_spi_read,
  311. };
  312. #endif /* CONFIG_SPI_MASTER */
  313. /*----------------------------------------------------------------------*/
  314. /* A given spi_device can represent up to eight mcp23sxx chips
  315. * sharing the same chipselect but using different addresses
  316. * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
  317. * Driver data holds all the per-chip data.
  318. */
  319. struct mcp23s08_driver_data {
  320. unsigned ngpio;
  321. struct mcp23s08 *mcp[8];
  322. struct mcp23s08 chip[];
  323. };
  324. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  325. {
  326. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  327. int status;
  328. mutex_lock(&mcp->lock);
  329. status = mcp_set_bit(mcp, MCP_IODIR, offset, true);
  330. mutex_unlock(&mcp->lock);
  331. return status;
  332. }
  333. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  334. {
  335. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  336. int status, ret;
  337. mutex_lock(&mcp->lock);
  338. /* REVISIT reading this clears any IRQ ... */
  339. ret = mcp_read(mcp, MCP_GPIO, &status);
  340. if (ret < 0)
  341. status = 0;
  342. else {
  343. mcp->cached_gpio = status;
  344. status = !!(status & (1 << offset));
  345. }
  346. mutex_unlock(&mcp->lock);
  347. return status;
  348. }
  349. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
  350. {
  351. return mcp_set_mask(mcp, MCP_OLAT, mask, value);
  352. }
  353. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  354. {
  355. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  356. unsigned mask = BIT(offset);
  357. mutex_lock(&mcp->lock);
  358. __mcp23s08_set(mcp, mask, !!value);
  359. mutex_unlock(&mcp->lock);
  360. }
  361. static int
  362. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  363. {
  364. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  365. unsigned mask = BIT(offset);
  366. int status;
  367. mutex_lock(&mcp->lock);
  368. status = __mcp23s08_set(mcp, mask, value);
  369. if (status == 0) {
  370. status = mcp_set_mask(mcp, MCP_IODIR, mask, false);
  371. }
  372. mutex_unlock(&mcp->lock);
  373. return status;
  374. }
  375. /*----------------------------------------------------------------------*/
  376. static irqreturn_t mcp23s08_irq(int irq, void *data)
  377. {
  378. struct mcp23s08 *mcp = data;
  379. int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
  380. unsigned int child_irq;
  381. bool intf_set, intcap_changed, gpio_bit_changed,
  382. defval_changed, gpio_set;
  383. mutex_lock(&mcp->lock);
  384. if (mcp_read(mcp, MCP_INTF, &intf))
  385. goto unlock;
  386. if (mcp_read(mcp, MCP_INTCAP, &intcap))
  387. goto unlock;
  388. if (mcp_read(mcp, MCP_INTCON, &intcon))
  389. goto unlock;
  390. if (mcp_read(mcp, MCP_DEFVAL, &defval))
  391. goto unlock;
  392. /* This clears the interrupt(configurable on S18) */
  393. if (mcp_read(mcp, MCP_GPIO, &gpio))
  394. goto unlock;
  395. gpio_orig = mcp->cached_gpio;
  396. mcp->cached_gpio = gpio;
  397. mutex_unlock(&mcp->lock);
  398. if (intf == 0) {
  399. /* There is no interrupt pending */
  400. return IRQ_HANDLED;
  401. }
  402. dev_dbg(mcp->chip.parent,
  403. "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
  404. intcap, intf, gpio_orig, gpio);
  405. for (i = 0; i < mcp->chip.ngpio; i++) {
  406. /* We must check all of the inputs on the chip,
  407. * otherwise we may not notice a change on >=2 pins.
  408. *
  409. * On at least the mcp23s17, INTCAP is only updated
  410. * one byte at a time(INTCAPA and INTCAPB are
  411. * not written to at the same time - only on a per-bank
  412. * basis).
  413. *
  414. * INTF only contains the single bit that caused the
  415. * interrupt per-bank. On the mcp23s17, there is
  416. * INTFA and INTFB. If two pins are changed on the A
  417. * side at the same time, INTF will only have one bit
  418. * set. If one pin on the A side and one pin on the B
  419. * side are changed at the same time, INTF will have
  420. * two bits set. Thus, INTF can't be the only check
  421. * to see if the input has changed.
  422. */
  423. intf_set = intf & BIT(i);
  424. if (i < 8 && intf_set)
  425. intcap_mask = 0x00FF;
  426. else if (i >= 8 && intf_set)
  427. intcap_mask = 0xFF00;
  428. else
  429. intcap_mask = 0x00;
  430. intcap_changed = (intcap_mask &
  431. (intcap & BIT(i))) !=
  432. (intcap_mask & (BIT(i) & gpio_orig));
  433. gpio_set = BIT(i) & gpio;
  434. gpio_bit_changed = (BIT(i) & gpio_orig) !=
  435. (BIT(i) & gpio);
  436. defval_changed = (BIT(i) & intcon) &&
  437. ((BIT(i) & gpio) !=
  438. (BIT(i) & defval));
  439. if (((gpio_bit_changed || intcap_changed) &&
  440. (BIT(i) & mcp->irq_rise) && gpio_set) ||
  441. ((gpio_bit_changed || intcap_changed) &&
  442. (BIT(i) & mcp->irq_fall) && !gpio_set) ||
  443. defval_changed) {
  444. child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
  445. handle_nested_irq(child_irq);
  446. }
  447. }
  448. return IRQ_HANDLED;
  449. unlock:
  450. mutex_unlock(&mcp->lock);
  451. return IRQ_HANDLED;
  452. }
  453. static void mcp23s08_irq_mask(struct irq_data *data)
  454. {
  455. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  456. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  457. unsigned int pos = data->hwirq;
  458. mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
  459. }
  460. static void mcp23s08_irq_unmask(struct irq_data *data)
  461. {
  462. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  463. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  464. unsigned int pos = data->hwirq;
  465. mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
  466. }
  467. static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
  468. {
  469. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  470. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  471. unsigned int pos = data->hwirq;
  472. int status = 0;
  473. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  474. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  475. mcp->irq_rise |= BIT(pos);
  476. mcp->irq_fall |= BIT(pos);
  477. } else if (type & IRQ_TYPE_EDGE_RISING) {
  478. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  479. mcp->irq_rise |= BIT(pos);
  480. mcp->irq_fall &= ~BIT(pos);
  481. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  482. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  483. mcp->irq_rise &= ~BIT(pos);
  484. mcp->irq_fall |= BIT(pos);
  485. } else if (type & IRQ_TYPE_LEVEL_HIGH) {
  486. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  487. mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
  488. } else if (type & IRQ_TYPE_LEVEL_LOW) {
  489. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  490. mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
  491. } else
  492. return -EINVAL;
  493. return status;
  494. }
  495. static void mcp23s08_irq_bus_lock(struct irq_data *data)
  496. {
  497. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  498. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  499. mutex_lock(&mcp->lock);
  500. regcache_cache_only(mcp->regmap, true);
  501. }
  502. static void mcp23s08_irq_bus_unlock(struct irq_data *data)
  503. {
  504. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  505. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  506. regcache_cache_only(mcp->regmap, false);
  507. regcache_sync(mcp->regmap);
  508. mutex_unlock(&mcp->lock);
  509. }
  510. static struct irq_chip mcp23s08_irq_chip = {
  511. .name = "gpio-mcp23xxx",
  512. .irq_mask = mcp23s08_irq_mask,
  513. .irq_unmask = mcp23s08_irq_unmask,
  514. .irq_set_type = mcp23s08_irq_set_type,
  515. .irq_bus_lock = mcp23s08_irq_bus_lock,
  516. .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
  517. };
  518. static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
  519. {
  520. struct gpio_chip *chip = &mcp->chip;
  521. int err;
  522. unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
  523. if (mcp->irq_active_high)
  524. irqflags |= IRQF_TRIGGER_HIGH;
  525. else
  526. irqflags |= IRQF_TRIGGER_LOW;
  527. err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
  528. mcp23s08_irq,
  529. irqflags, dev_name(chip->parent), mcp);
  530. if (err != 0) {
  531. dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
  532. mcp->irq, err);
  533. return err;
  534. }
  535. return 0;
  536. }
  537. static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp)
  538. {
  539. struct gpio_chip *chip = &mcp->chip;
  540. int err;
  541. err = gpiochip_irqchip_add_nested(chip,
  542. &mcp23s08_irq_chip,
  543. 0,
  544. handle_simple_irq,
  545. IRQ_TYPE_NONE);
  546. if (err) {
  547. dev_err(chip->parent,
  548. "could not connect irqchip to gpiochip: %d\n", err);
  549. return err;
  550. }
  551. gpiochip_set_nested_irqchip(chip,
  552. &mcp23s08_irq_chip,
  553. mcp->irq);
  554. return 0;
  555. }
  556. /*----------------------------------------------------------------------*/
  557. #ifdef CONFIG_DEBUG_FS
  558. #include <linux/seq_file.h>
  559. /*
  560. * This compares the chip's registers with the register
  561. * cache and corrects any incorrectly set register. This
  562. * can be used to fix state for MCP23xxx, that temporary
  563. * lost its power supply.
  564. */
  565. #define MCP23S08_CONFIG_REGS 7
  566. static int __check_mcp23s08_reg_cache(struct mcp23s08 *mcp)
  567. {
  568. int cached[MCP23S08_CONFIG_REGS];
  569. int err = 0, i;
  570. /* read cached config registers */
  571. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  572. err = mcp_read(mcp, i, &cached[i]);
  573. if (err)
  574. goto out;
  575. }
  576. regcache_cache_bypass(mcp->regmap, true);
  577. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  578. int uncached;
  579. err = mcp_read(mcp, i, &uncached);
  580. if (err)
  581. goto out;
  582. if (uncached != cached[i]) {
  583. dev_err(mcp->dev, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n",
  584. i, uncached, cached[i]);
  585. mcp_write(mcp, i, cached[i]);
  586. }
  587. }
  588. out:
  589. if (err)
  590. dev_err(mcp->dev, "read error: reg=%02x, err=%d", i, err);
  591. regcache_cache_bypass(mcp->regmap, false);
  592. return err;
  593. }
  594. /*
  595. * This shows more info than the generic gpio dump code:
  596. * pullups, deglitching, open drain drive.
  597. */
  598. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  599. {
  600. struct mcp23s08 *mcp;
  601. char bank;
  602. int t;
  603. unsigned mask;
  604. int iodir, gpio, gppu;
  605. mcp = gpiochip_get_data(chip);
  606. /* NOTE: we only handle one bank for now ... */
  607. bank = '0' + ((mcp->addr >> 1) & 0x7);
  608. mutex_lock(&mcp->lock);
  609. t = __check_mcp23s08_reg_cache(mcp);
  610. if (t) {
  611. seq_printf(s, " I/O Error\n");
  612. goto done;
  613. }
  614. t = mcp_read(mcp, MCP_IODIR, &iodir);
  615. if (t) {
  616. seq_printf(s, " I/O Error\n");
  617. goto done;
  618. }
  619. t = mcp_read(mcp, MCP_GPIO, &gpio);
  620. if (t) {
  621. seq_printf(s, " I/O Error\n");
  622. goto done;
  623. }
  624. t = mcp_read(mcp, MCP_GPPU, &gppu);
  625. if (t) {
  626. seq_printf(s, " I/O Error\n");
  627. goto done;
  628. }
  629. for (t = 0, mask = BIT(0); t < chip->ngpio; t++, mask <<= 1) {
  630. const char *label;
  631. label = gpiochip_is_requested(chip, t);
  632. if (!label)
  633. continue;
  634. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s\n",
  635. chip->base + t, bank, t, label,
  636. (iodir & mask) ? "in " : "out",
  637. (gpio & mask) ? "hi" : "lo",
  638. (gppu & mask) ? "up" : " ");
  639. /* NOTE: ignoring the irq-related registers */
  640. }
  641. done:
  642. mutex_unlock(&mcp->lock);
  643. }
  644. #else
  645. #define mcp23s08_dbg_show NULL
  646. #endif
  647. /*----------------------------------------------------------------------*/
  648. static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  649. void *data, unsigned addr, unsigned type,
  650. unsigned int base, int cs)
  651. {
  652. int status, ret;
  653. bool mirror = false;
  654. bool open_drain = false;
  655. struct regmap_config *one_regmap_config = NULL;
  656. int raw_chip_address = (addr & ~0x40) >> 1;
  657. mutex_init(&mcp->lock);
  658. mcp->dev = dev;
  659. mcp->addr = addr;
  660. mcp->irq_active_high = false;
  661. mcp->chip.direction_input = mcp23s08_direction_input;
  662. mcp->chip.get = mcp23s08_get;
  663. mcp->chip.direction_output = mcp23s08_direction_output;
  664. mcp->chip.set = mcp23s08_set;
  665. mcp->chip.dbg_show = mcp23s08_dbg_show;
  666. #ifdef CONFIG_OF_GPIO
  667. mcp->chip.of_gpio_n_cells = 2;
  668. mcp->chip.of_node = dev->of_node;
  669. #endif
  670. switch (type) {
  671. #ifdef CONFIG_SPI_MASTER
  672. case MCP_TYPE_S08:
  673. case MCP_TYPE_S17:
  674. switch (type) {
  675. case MCP_TYPE_S08:
  676. one_regmap_config =
  677. devm_kmemdup(dev, &mcp23x08_regmap,
  678. sizeof(struct regmap_config), GFP_KERNEL);
  679. mcp->reg_shift = 0;
  680. mcp->chip.ngpio = 8;
  681. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
  682. "mcp23s08.%d", raw_chip_address);
  683. break;
  684. case MCP_TYPE_S17:
  685. one_regmap_config =
  686. devm_kmemdup(dev, &mcp23x17_regmap,
  687. sizeof(struct regmap_config), GFP_KERNEL);
  688. mcp->reg_shift = 1;
  689. mcp->chip.ngpio = 16;
  690. mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
  691. "mcp23s17.%d", raw_chip_address);
  692. break;
  693. }
  694. if (!one_regmap_config)
  695. return -ENOMEM;
  696. one_regmap_config->name = devm_kasprintf(dev, GFP_KERNEL, "%d", raw_chip_address);
  697. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  698. one_regmap_config);
  699. break;
  700. case MCP_TYPE_S18:
  701. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  702. &mcp23x17_regmap);
  703. mcp->reg_shift = 1;
  704. mcp->chip.ngpio = 16;
  705. mcp->chip.label = "mcp23s18";
  706. break;
  707. #endif /* CONFIG_SPI_MASTER */
  708. #if IS_ENABLED(CONFIG_I2C)
  709. case MCP_TYPE_008:
  710. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
  711. mcp->reg_shift = 0;
  712. mcp->chip.ngpio = 8;
  713. mcp->chip.label = "mcp23008";
  714. break;
  715. case MCP_TYPE_017:
  716. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
  717. mcp->reg_shift = 1;
  718. mcp->chip.ngpio = 16;
  719. mcp->chip.label = "mcp23017";
  720. break;
  721. case MCP_TYPE_018:
  722. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
  723. mcp->reg_shift = 1;
  724. mcp->chip.ngpio = 16;
  725. mcp->chip.label = "mcp23018";
  726. break;
  727. #endif /* CONFIG_I2C */
  728. default:
  729. dev_err(dev, "invalid device type (%d)\n", type);
  730. return -EINVAL;
  731. }
  732. if (IS_ERR(mcp->regmap))
  733. return PTR_ERR(mcp->regmap);
  734. mcp->chip.base = base;
  735. mcp->chip.can_sleep = true;
  736. mcp->chip.parent = dev;
  737. mcp->chip.owner = THIS_MODULE;
  738. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  739. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  740. */
  741. ret = mcp_read(mcp, MCP_IOCON, &status);
  742. if (ret < 0)
  743. goto fail;
  744. mcp->irq_controller =
  745. device_property_read_bool(dev, "interrupt-controller");
  746. if (mcp->irq && mcp->irq_controller) {
  747. mcp->irq_active_high =
  748. device_property_read_bool(dev,
  749. "microchip,irq-active-high");
  750. mirror = device_property_read_bool(dev, "microchip,irq-mirror");
  751. open_drain = device_property_read_bool(dev, "drive-open-drain");
  752. }
  753. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
  754. mcp->irq_active_high || open_drain) {
  755. /* mcp23s17 has IOCON twice, make sure they are in sync */
  756. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  757. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  758. if (mcp->irq_active_high)
  759. status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
  760. else
  761. status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
  762. if (mirror)
  763. status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
  764. if (open_drain)
  765. status |= IOCON_ODR | (IOCON_ODR << 8);
  766. if (type == MCP_TYPE_S18 || type == MCP_TYPE_018)
  767. status |= IOCON_INTCC | (IOCON_INTCC << 8);
  768. ret = mcp_write(mcp, MCP_IOCON, status);
  769. if (ret < 0)
  770. goto fail;
  771. }
  772. if (mcp->irq && mcp->irq_controller) {
  773. ret = mcp23s08_irqchip_setup(mcp);
  774. if (ret)
  775. goto fail;
  776. }
  777. ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
  778. if (ret < 0)
  779. goto fail;
  780. if (one_regmap_config) {
  781. mcp->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
  782. "mcp23xxx-pinctrl.%d", raw_chip_address);
  783. if (!mcp->pinctrl_desc.name)
  784. return -ENOMEM;
  785. } else {
  786. mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
  787. }
  788. mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
  789. mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
  790. mcp->pinctrl_desc.npins = mcp->chip.ngpio;
  791. if (mcp->pinctrl_desc.npins == 8)
  792. mcp->pinctrl_desc.pins = mcp23x08_pins;
  793. else if (mcp->pinctrl_desc.npins == 16)
  794. mcp->pinctrl_desc.pins = mcp23x17_pins;
  795. mcp->pinctrl_desc.owner = THIS_MODULE;
  796. mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
  797. if (IS_ERR(mcp->pctldev)) {
  798. ret = PTR_ERR(mcp->pctldev);
  799. goto fail;
  800. }
  801. if (mcp->irq)
  802. ret = mcp23s08_irq_setup(mcp);
  803. fail:
  804. if (ret < 0)
  805. dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
  806. return ret;
  807. }
  808. /*----------------------------------------------------------------------*/
  809. #ifdef CONFIG_OF
  810. #ifdef CONFIG_SPI_MASTER
  811. static const struct of_device_id mcp23s08_spi_of_match[] = {
  812. {
  813. .compatible = "microchip,mcp23s08",
  814. .data = (void *) MCP_TYPE_S08,
  815. },
  816. {
  817. .compatible = "microchip,mcp23s17",
  818. .data = (void *) MCP_TYPE_S17,
  819. },
  820. {
  821. .compatible = "microchip,mcp23s18",
  822. .data = (void *) MCP_TYPE_S18,
  823. },
  824. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  825. {
  826. .compatible = "mcp,mcp23s08",
  827. .data = (void *) MCP_TYPE_S08,
  828. },
  829. {
  830. .compatible = "mcp,mcp23s17",
  831. .data = (void *) MCP_TYPE_S17,
  832. },
  833. { },
  834. };
  835. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  836. #endif
  837. #if IS_ENABLED(CONFIG_I2C)
  838. static const struct of_device_id mcp23s08_i2c_of_match[] = {
  839. {
  840. .compatible = "microchip,mcp23008",
  841. .data = (void *) MCP_TYPE_008,
  842. },
  843. {
  844. .compatible = "microchip,mcp23017",
  845. .data = (void *) MCP_TYPE_017,
  846. },
  847. {
  848. .compatible = "microchip,mcp23018",
  849. .data = (void *) MCP_TYPE_018,
  850. },
  851. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  852. {
  853. .compatible = "mcp,mcp23008",
  854. .data = (void *) MCP_TYPE_008,
  855. },
  856. {
  857. .compatible = "mcp,mcp23017",
  858. .data = (void *) MCP_TYPE_017,
  859. },
  860. { },
  861. };
  862. MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
  863. #endif
  864. #endif /* CONFIG_OF */
  865. #if IS_ENABLED(CONFIG_I2C)
  866. static int mcp230xx_probe(struct i2c_client *client,
  867. const struct i2c_device_id *id)
  868. {
  869. struct mcp23s08_platform_data *pdata, local_pdata;
  870. struct mcp23s08 *mcp;
  871. int status;
  872. pdata = dev_get_platdata(&client->dev);
  873. if (!pdata) {
  874. pdata = &local_pdata;
  875. pdata->base = -1;
  876. }
  877. mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL);
  878. if (!mcp)
  879. return -ENOMEM;
  880. mcp->irq = client->irq;
  881. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  882. id->driver_data, pdata->base, 0);
  883. if (status)
  884. return status;
  885. i2c_set_clientdata(client, mcp);
  886. return 0;
  887. }
  888. static const struct i2c_device_id mcp230xx_id[] = {
  889. { "mcp23008", MCP_TYPE_008 },
  890. { "mcp23017", MCP_TYPE_017 },
  891. { "mcp23018", MCP_TYPE_018 },
  892. { },
  893. };
  894. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  895. static struct i2c_driver mcp230xx_driver = {
  896. .driver = {
  897. .name = "mcp230xx",
  898. .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
  899. },
  900. .probe = mcp230xx_probe,
  901. .id_table = mcp230xx_id,
  902. };
  903. static int __init mcp23s08_i2c_init(void)
  904. {
  905. return i2c_add_driver(&mcp230xx_driver);
  906. }
  907. static void mcp23s08_i2c_exit(void)
  908. {
  909. i2c_del_driver(&mcp230xx_driver);
  910. }
  911. #else
  912. static int __init mcp23s08_i2c_init(void) { return 0; }
  913. static void mcp23s08_i2c_exit(void) { }
  914. #endif /* CONFIG_I2C */
  915. /*----------------------------------------------------------------------*/
  916. #ifdef CONFIG_SPI_MASTER
  917. static int mcp23s08_probe(struct spi_device *spi)
  918. {
  919. struct mcp23s08_platform_data *pdata, local_pdata;
  920. unsigned addr;
  921. int chips = 0;
  922. struct mcp23s08_driver_data *data;
  923. int status, type;
  924. unsigned ngpio = 0;
  925. const struct of_device_id *match;
  926. match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
  927. if (match)
  928. type = (int)(uintptr_t)match->data;
  929. else
  930. type = spi_get_device_id(spi)->driver_data;
  931. pdata = dev_get_platdata(&spi->dev);
  932. if (!pdata) {
  933. pdata = &local_pdata;
  934. pdata->base = -1;
  935. status = device_property_read_u32(&spi->dev,
  936. "microchip,spi-present-mask", &pdata->spi_present_mask);
  937. if (status) {
  938. status = device_property_read_u32(&spi->dev,
  939. "mcp,spi-present-mask",
  940. &pdata->spi_present_mask);
  941. if (status) {
  942. dev_err(&spi->dev, "missing spi-present-mask");
  943. return -ENODEV;
  944. }
  945. }
  946. }
  947. if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) {
  948. dev_err(&spi->dev, "invalid spi-present-mask");
  949. return -ENODEV;
  950. }
  951. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  952. if (pdata->spi_present_mask & BIT(addr))
  953. chips++;
  954. }
  955. if (!chips)
  956. return -ENODEV;
  957. data = devm_kzalloc(&spi->dev,
  958. sizeof(*data) + chips * sizeof(struct mcp23s08),
  959. GFP_KERNEL);
  960. if (!data)
  961. return -ENOMEM;
  962. spi_set_drvdata(spi, data);
  963. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  964. if (!(pdata->spi_present_mask & BIT(addr)))
  965. continue;
  966. chips--;
  967. data->mcp[addr] = &data->chip[chips];
  968. data->mcp[addr]->irq = spi->irq;
  969. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  970. 0x40 | (addr << 1), type,
  971. pdata->base, addr);
  972. if (status < 0)
  973. return status;
  974. if (pdata->base != -1)
  975. pdata->base += data->mcp[addr]->chip.ngpio;
  976. ngpio += data->mcp[addr]->chip.ngpio;
  977. }
  978. data->ngpio = ngpio;
  979. return 0;
  980. }
  981. static const struct spi_device_id mcp23s08_ids[] = {
  982. { "mcp23s08", MCP_TYPE_S08 },
  983. { "mcp23s17", MCP_TYPE_S17 },
  984. { "mcp23s18", MCP_TYPE_S18 },
  985. { },
  986. };
  987. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  988. static struct spi_driver mcp23s08_driver = {
  989. .probe = mcp23s08_probe,
  990. .id_table = mcp23s08_ids,
  991. .driver = {
  992. .name = "mcp23s08",
  993. .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
  994. },
  995. };
  996. static int __init mcp23s08_spi_init(void)
  997. {
  998. return spi_register_driver(&mcp23s08_driver);
  999. }
  1000. static void mcp23s08_spi_exit(void)
  1001. {
  1002. spi_unregister_driver(&mcp23s08_driver);
  1003. }
  1004. #else
  1005. static int __init mcp23s08_spi_init(void) { return 0; }
  1006. static void mcp23s08_spi_exit(void) { }
  1007. #endif /* CONFIG_SPI_MASTER */
  1008. /*----------------------------------------------------------------------*/
  1009. static int __init mcp23s08_init(void)
  1010. {
  1011. int ret;
  1012. ret = mcp23s08_spi_init();
  1013. if (ret)
  1014. goto spi_fail;
  1015. ret = mcp23s08_i2c_init();
  1016. if (ret)
  1017. goto i2c_fail;
  1018. return 0;
  1019. i2c_fail:
  1020. mcp23s08_spi_exit();
  1021. spi_fail:
  1022. return ret;
  1023. }
  1024. /* register after spi/i2c postcore initcall and before
  1025. * subsys initcalls that may rely on these GPIOs
  1026. */
  1027. subsys_initcall(mcp23s08_init);
  1028. static void __exit mcp23s08_exit(void)
  1029. {
  1030. mcp23s08_spi_exit();
  1031. mcp23s08_i2c_exit();
  1032. }
  1033. module_exit(mcp23s08_exit);
  1034. MODULE_LICENSE("GPL");