mcu_mpc8349emitx.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Power Management and GPIO expander driver for MPC8349E-mITX-compatible MCU
  3. *
  4. * Copyright (c) 2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/mutex.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/slab.h>
  23. #include <asm/prom.h>
  24. #include <asm/machdep.h>
  25. /*
  26. * I don't have specifications for the MCU firmware, I found this register
  27. * and bits positions by the trial&error method.
  28. */
  29. #define MCU_REG_CTRL 0x20
  30. #define MCU_CTRL_POFF 0x40
  31. #define MCU_NUM_GPIO 2
  32. struct mcu {
  33. struct mutex lock;
  34. struct device_node *np;
  35. struct i2c_client *client;
  36. struct of_gpio_chip of_gc;
  37. u8 reg_ctrl;
  38. };
  39. static struct mcu *glob_mcu;
  40. static void mcu_power_off(void)
  41. {
  42. struct mcu *mcu = glob_mcu;
  43. pr_info("Sending power-off request to the MCU...\n");
  44. mutex_lock(&mcu->lock);
  45. i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL,
  46. mcu->reg_ctrl | MCU_CTRL_POFF);
  47. mutex_unlock(&mcu->lock);
  48. }
  49. static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  50. {
  51. struct of_gpio_chip *of_gc = to_of_gpio_chip(gc);
  52. struct mcu *mcu = container_of(of_gc, struct mcu, of_gc);
  53. u8 bit = 1 << (4 + gpio);
  54. mutex_lock(&mcu->lock);
  55. if (val)
  56. mcu->reg_ctrl &= ~bit;
  57. else
  58. mcu->reg_ctrl |= bit;
  59. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
  60. mutex_unlock(&mcu->lock);
  61. }
  62. static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  63. {
  64. mcu_gpio_set(gc, gpio, val);
  65. return 0;
  66. }
  67. static int mcu_gpiochip_add(struct mcu *mcu)
  68. {
  69. struct device_node *np;
  70. struct of_gpio_chip *of_gc = &mcu->of_gc;
  71. struct gpio_chip *gc = &of_gc->gc;
  72. int ret;
  73. np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
  74. if (!np)
  75. return -ENODEV;
  76. gc->owner = THIS_MODULE;
  77. gc->label = np->full_name;
  78. gc->can_sleep = 1;
  79. gc->ngpio = MCU_NUM_GPIO;
  80. gc->base = -1;
  81. gc->set = mcu_gpio_set;
  82. gc->direction_output = mcu_gpio_dir_out;
  83. of_gc->gpio_cells = 2;
  84. of_gc->xlate = of_gpio_simple_xlate;
  85. np->data = of_gc;
  86. mcu->np = np;
  87. /*
  88. * We don't want to lose the node, its ->data and ->full_name...
  89. * So, if succeeded, we don't put the node here.
  90. */
  91. ret = gpiochip_add(gc);
  92. if (ret)
  93. of_node_put(np);
  94. return ret;
  95. }
  96. static int mcu_gpiochip_remove(struct mcu *mcu)
  97. {
  98. int ret;
  99. ret = gpiochip_remove(&mcu->of_gc.gc);
  100. if (ret)
  101. return ret;
  102. of_node_put(mcu->np);
  103. return 0;
  104. }
  105. static int __devinit mcu_probe(struct i2c_client *client,
  106. const struct i2c_device_id *id)
  107. {
  108. struct mcu *mcu;
  109. int ret;
  110. mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
  111. if (!mcu)
  112. return -ENOMEM;
  113. mutex_init(&mcu->lock);
  114. mcu->client = client;
  115. i2c_set_clientdata(client, mcu);
  116. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  117. if (ret < 0)
  118. goto err;
  119. mcu->reg_ctrl = ret;
  120. ret = mcu_gpiochip_add(mcu);
  121. if (ret)
  122. goto err;
  123. /* XXX: this is potentially racy, but there is no lock for ppc_md */
  124. if (!ppc_md.power_off) {
  125. glob_mcu = mcu;
  126. ppc_md.power_off = mcu_power_off;
  127. dev_info(&client->dev, "will provide power-off service\n");
  128. }
  129. return 0;
  130. err:
  131. kfree(mcu);
  132. return ret;
  133. }
  134. static int __devexit mcu_remove(struct i2c_client *client)
  135. {
  136. struct mcu *mcu = i2c_get_clientdata(client);
  137. int ret;
  138. if (glob_mcu == mcu) {
  139. ppc_md.power_off = NULL;
  140. glob_mcu = NULL;
  141. }
  142. ret = mcu_gpiochip_remove(mcu);
  143. if (ret)
  144. return ret;
  145. i2c_set_clientdata(client, NULL);
  146. kfree(mcu);
  147. return 0;
  148. }
  149. static const struct i2c_device_id mcu_ids[] = {
  150. { "mcu-mpc8349emitx", },
  151. {},
  152. };
  153. MODULE_DEVICE_TABLE(i2c, mcu_ids);
  154. static struct i2c_driver mcu_driver = {
  155. .driver = {
  156. .name = "mcu-mpc8349emitx",
  157. .owner = THIS_MODULE,
  158. },
  159. .probe = mcu_probe,
  160. .remove = __devexit_p(mcu_remove),
  161. .id_table = mcu_ids,
  162. };
  163. static int __init mcu_init(void)
  164. {
  165. return i2c_add_driver(&mcu_driver);
  166. }
  167. module_init(mcu_init);
  168. static void __exit mcu_exit(void)
  169. {
  170. i2c_del_driver(&mcu_driver);
  171. }
  172. module_exit(mcu_exit);
  173. MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
  174. "MPC8349E-mITX-compatible MCU");
  175. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  176. MODULE_LICENSE("GPL");