mcu_mpc8349emitx.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 i2c_client *client;
  35. struct gpio_chip gc;
  36. u8 reg_ctrl;
  37. };
  38. static struct mcu *glob_mcu;
  39. static void mcu_power_off(void)
  40. {
  41. struct mcu *mcu = glob_mcu;
  42. pr_info("Sending power-off request to the MCU...\n");
  43. mutex_lock(&mcu->lock);
  44. i2c_smbus_write_byte_data(glob_mcu->client, MCU_REG_CTRL,
  45. mcu->reg_ctrl | MCU_CTRL_POFF);
  46. mutex_unlock(&mcu->lock);
  47. }
  48. static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  49. {
  50. struct mcu *mcu = container_of(gc, struct mcu, gc);
  51. u8 bit = 1 << (4 + gpio);
  52. mutex_lock(&mcu->lock);
  53. if (val)
  54. mcu->reg_ctrl &= ~bit;
  55. else
  56. mcu->reg_ctrl |= bit;
  57. i2c_smbus_write_byte_data(mcu->client, MCU_REG_CTRL, mcu->reg_ctrl);
  58. mutex_unlock(&mcu->lock);
  59. }
  60. static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  61. {
  62. mcu_gpio_set(gc, gpio, val);
  63. return 0;
  64. }
  65. static int mcu_gpiochip_add(struct mcu *mcu)
  66. {
  67. struct device_node *np;
  68. struct gpio_chip *gc = &mcu->gc;
  69. np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
  70. if (!np)
  71. return -ENODEV;
  72. gc->owner = THIS_MODULE;
  73. gc->label = np->full_name;
  74. gc->can_sleep = 1;
  75. gc->ngpio = MCU_NUM_GPIO;
  76. gc->base = -1;
  77. gc->set = mcu_gpio_set;
  78. gc->direction_output = mcu_gpio_dir_out;
  79. gc->of_node = np;
  80. return gpiochip_add(gc);
  81. }
  82. static int mcu_gpiochip_remove(struct mcu *mcu)
  83. {
  84. return gpiochip_remove(&mcu->gc);
  85. }
  86. static int __devinit mcu_probe(struct i2c_client *client,
  87. const struct i2c_device_id *id)
  88. {
  89. struct mcu *mcu;
  90. int ret;
  91. mcu = kzalloc(sizeof(*mcu), GFP_KERNEL);
  92. if (!mcu)
  93. return -ENOMEM;
  94. mutex_init(&mcu->lock);
  95. mcu->client = client;
  96. i2c_set_clientdata(client, mcu);
  97. ret = i2c_smbus_read_byte_data(mcu->client, MCU_REG_CTRL);
  98. if (ret < 0)
  99. goto err;
  100. mcu->reg_ctrl = ret;
  101. ret = mcu_gpiochip_add(mcu);
  102. if (ret)
  103. goto err;
  104. /* XXX: this is potentially racy, but there is no lock for ppc_md */
  105. if (!ppc_md.power_off) {
  106. glob_mcu = mcu;
  107. ppc_md.power_off = mcu_power_off;
  108. dev_info(&client->dev, "will provide power-off service\n");
  109. }
  110. return 0;
  111. err:
  112. kfree(mcu);
  113. return ret;
  114. }
  115. static int __devexit mcu_remove(struct i2c_client *client)
  116. {
  117. struct mcu *mcu = i2c_get_clientdata(client);
  118. int ret;
  119. if (glob_mcu == mcu) {
  120. ppc_md.power_off = NULL;
  121. glob_mcu = NULL;
  122. }
  123. ret = mcu_gpiochip_remove(mcu);
  124. if (ret)
  125. return ret;
  126. i2c_set_clientdata(client, NULL);
  127. kfree(mcu);
  128. return 0;
  129. }
  130. static const struct i2c_device_id mcu_ids[] = {
  131. { "mcu-mpc8349emitx", },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(i2c, mcu_ids);
  135. static struct of_device_id mcu_of_match_table[] __devinitdata = {
  136. { .compatible = "fsl,mcu-mpc8349emitx", },
  137. { },
  138. };
  139. static struct i2c_driver mcu_driver = {
  140. .driver = {
  141. .name = "mcu-mpc8349emitx",
  142. .owner = THIS_MODULE,
  143. .of_match_table = mcu_of_match_table,
  144. },
  145. .probe = mcu_probe,
  146. .remove = __devexit_p(mcu_remove),
  147. .id_table = mcu_ids,
  148. };
  149. static int __init mcu_init(void)
  150. {
  151. return i2c_add_driver(&mcu_driver);
  152. }
  153. module_init(mcu_init);
  154. static void __exit mcu_exit(void)
  155. {
  156. i2c_del_driver(&mcu_driver);
  157. }
  158. module_exit(mcu_exit);
  159. MODULE_DESCRIPTION("Power Management and GPIO expander driver for "
  160. "MPC8349E-mITX-compatible MCU");
  161. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  162. MODULE_LICENSE("GPL");