gpio-mc9s08dz60.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio/driver.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 void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit)
  29. {
  30. *reg = 0x20 + offset / GPIO_NUM_PER_GROUP;
  31. *bit = offset % GPIO_NUM_PER_GROUP;
  32. }
  33. static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset)
  34. {
  35. u8 reg, bit;
  36. s32 value;
  37. struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
  38. mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  39. value = i2c_smbus_read_byte_data(mc9s->client, reg);
  40. return (value >= 0) ? (value >> bit) & 0x1 : 0;
  41. }
  42. static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val)
  43. {
  44. u8 reg, bit;
  45. s32 value;
  46. mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  47. value = i2c_smbus_read_byte_data(mc9s->client, reg);
  48. if (value >= 0) {
  49. if (val)
  50. value |= 1 << bit;
  51. else
  52. value &= ~(1 << bit);
  53. return i2c_smbus_write_byte_data(mc9s->client, reg, value);
  54. } else
  55. return value;
  56. }
  57. static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val)
  58. {
  59. struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
  60. mc9s08dz60_set(mc9s, offset, val);
  61. }
  62. static int mc9s08dz60_direction_output(struct gpio_chip *gc,
  63. unsigned offset, int val)
  64. {
  65. struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
  66. return mc9s08dz60_set(mc9s, offset, val);
  67. }
  68. static int mc9s08dz60_probe(struct i2c_client *client,
  69. const struct i2c_device_id *id)
  70. {
  71. struct mc9s08dz60 *mc9s;
  72. mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL);
  73. if (!mc9s)
  74. return -ENOMEM;
  75. mc9s->chip.label = client->name;
  76. mc9s->chip.base = -1;
  77. mc9s->chip.parent = &client->dev;
  78. mc9s->chip.owner = THIS_MODULE;
  79. mc9s->chip.ngpio = GPIO_NUM;
  80. mc9s->chip.can_sleep = true;
  81. mc9s->chip.get = mc9s08dz60_get_value;
  82. mc9s->chip.set = mc9s08dz60_set_value;
  83. mc9s->chip.direction_output = mc9s08dz60_direction_output;
  84. mc9s->client = client;
  85. i2c_set_clientdata(client, mc9s);
  86. return devm_gpiochip_add_data(&client->dev, &mc9s->chip, mc9s);
  87. }
  88. static const struct i2c_device_id mc9s08dz60_id[] = {
  89. {"mc9s08dz60", 0},
  90. {},
  91. };
  92. static struct i2c_driver mc9s08dz60_i2c_driver = {
  93. .driver = {
  94. .name = "mc9s08dz60",
  95. },
  96. .probe = mc9s08dz60_probe,
  97. .id_table = mc9s08dz60_id,
  98. };
  99. builtin_i2c_driver(mc9s08dz60_i2c_driver);