gpio-mc9s08dz60.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * Author: Wu Guoxing <b39297@freescale.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio.h>
  21. #define GPIO_GROUP_NUM 2
  22. #define GPIO_NUM_PER_GROUP 8
  23. #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP)
  24. struct mc9s08dz60 {
  25. struct i2c_client *client;
  26. struct gpio_chip chip;
  27. };
  28. static inline struct mc9s08dz60 *to_mc9s08dz60(struct gpio_chip *gc)
  29. {
  30. return container_of(gc, struct mc9s08dz60, chip);
  31. }
  32. static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit)
  33. {
  34. *reg = 0x20 + offset / GPIO_NUM_PER_GROUP;
  35. *bit = offset % GPIO_NUM_PER_GROUP;
  36. }
  37. static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset)
  38. {
  39. u8 reg, bit;
  40. s32 value;
  41. struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc);
  42. mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  43. value = i2c_smbus_read_byte_data(mc9s->client, reg);
  44. return (value >= 0) ? (value >> bit) & 0x1 : 0;
  45. }
  46. static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val)
  47. {
  48. u8 reg, bit;
  49. s32 value;
  50. mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  51. value = i2c_smbus_read_byte_data(mc9s->client, reg);
  52. if (value >= 0) {
  53. if (val)
  54. value |= 1 << bit;
  55. else
  56. value &= ~(1 << bit);
  57. return i2c_smbus_write_byte_data(mc9s->client, reg, value);
  58. } else
  59. return value;
  60. }
  61. static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val)
  62. {
  63. struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc);
  64. mc9s08dz60_set(mc9s, offset, val);
  65. }
  66. static int mc9s08dz60_direction_output(struct gpio_chip *gc,
  67. unsigned offset, int val)
  68. {
  69. struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc);
  70. return mc9s08dz60_set(mc9s, offset, val);
  71. }
  72. static int mc9s08dz60_probe(struct i2c_client *client,
  73. const struct i2c_device_id *id)
  74. {
  75. struct mc9s08dz60 *mc9s;
  76. mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL);
  77. if (!mc9s)
  78. return -ENOMEM;
  79. mc9s->chip.label = client->name;
  80. mc9s->chip.base = -1;
  81. mc9s->chip.dev = &client->dev;
  82. mc9s->chip.owner = THIS_MODULE;
  83. mc9s->chip.ngpio = GPIO_NUM;
  84. mc9s->chip.can_sleep = true;
  85. mc9s->chip.get = mc9s08dz60_get_value;
  86. mc9s->chip.set = mc9s08dz60_set_value;
  87. mc9s->chip.direction_output = mc9s08dz60_direction_output;
  88. mc9s->client = client;
  89. i2c_set_clientdata(client, mc9s);
  90. return gpiochip_add(&mc9s->chip);
  91. }
  92. static int mc9s08dz60_remove(struct i2c_client *client)
  93. {
  94. struct mc9s08dz60 *mc9s;
  95. mc9s = i2c_get_clientdata(client);
  96. gpiochip_remove(&mc9s->chip);
  97. return 0;
  98. }
  99. static const struct i2c_device_id mc9s08dz60_id[] = {
  100. {"mc9s08dz60", 0},
  101. {},
  102. };
  103. MODULE_DEVICE_TABLE(i2c, mc9s08dz60_id);
  104. static struct i2c_driver mc9s08dz60_i2c_driver = {
  105. .driver = {
  106. .owner = THIS_MODULE,
  107. .name = "mc9s08dz60",
  108. },
  109. .probe = mc9s08dz60_probe,
  110. .remove = mc9s08dz60_remove,
  111. .id_table = mc9s08dz60_id,
  112. };
  113. module_i2c_driver(mc9s08dz60_i2c_driver);
  114. MODULE_AUTHOR("Freescale Semiconductor, Inc. "
  115. "Wu Guoxing <b39297@freescale.com>");
  116. MODULE_DESCRIPTION("mc9s08dz60 gpio function on mx35 3ds board");
  117. MODULE_LICENSE("GPL v2");