wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 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 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/gpio.h> |
| 22 | |
| 23 | #define GPIO_GROUP_NUM 2 |
| 24 | #define GPIO_NUM_PER_GROUP 8 |
| 25 | #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP) |
| 26 | |
| 27 | struct mc9s08dz60 { |
| 28 | struct i2c_client *client; |
| 29 | struct gpio_chip chip; |
| 30 | }; |
| 31 | |
| 32 | static inline struct mc9s08dz60 *to_mc9s08dz60(struct gpio_chip *gc) |
| 33 | { |
| 34 | return container_of(gc, struct mc9s08dz60, chip); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit) |
| 39 | { |
| 40 | *reg = 0x20 + offset / GPIO_NUM_PER_GROUP; |
| 41 | *bit = offset % GPIO_NUM_PER_GROUP; |
| 42 | } |
| 43 | |
| 44 | static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset) |
| 45 | { |
| 46 | u8 reg, bit; |
| 47 | s32 value; |
| 48 | struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc); |
| 49 | |
| 50 | mc9s_gpio_to_reg_and_bit(offset, ®, &bit); |
| 51 | value = i2c_smbus_read_byte_data(mc9s->client, reg); |
| 52 | |
| 53 | return (value >= 0) ? (value >> bit) & 0x1 : 0; |
| 54 | } |
| 55 | |
| 56 | static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val) |
| 57 | { |
| 58 | u8 reg, bit; |
| 59 | s32 value; |
| 60 | |
| 61 | mc9s_gpio_to_reg_and_bit(offset, ®, &bit); |
| 62 | value = i2c_smbus_read_byte_data(mc9s->client, reg); |
| 63 | if (value >= 0) { |
| 64 | if (val) |
| 65 | value |= 1 << bit; |
| 66 | else |
| 67 | value &= ~(1 << bit); |
| 68 | |
| 69 | return i2c_smbus_write_byte_data(mc9s->client, reg, value); |
| 70 | } else |
| 71 | return value; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val) |
| 77 | { |
| 78 | struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc); |
| 79 | |
| 80 | mc9s08dz60_set(mc9s, offset, val); |
| 81 | } |
| 82 | |
| 83 | static int mc9s08dz60_direction_output(struct gpio_chip *gc, |
| 84 | unsigned offset, int val) |
| 85 | { |
| 86 | struct mc9s08dz60 *mc9s = to_mc9s08dz60(gc); |
| 87 | |
| 88 | return mc9s08dz60_set(mc9s, offset, val); |
| 89 | } |
| 90 | |
| 91 | static int mc9s08dz60_probe(struct i2c_client *client, |
| 92 | const struct i2c_device_id *id) |
| 93 | { |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 94 | struct mc9s08dz60 *mc9s; |
| 95 | |
Axel Lin | 3c7051d | 2012-08-29 09:36:54 +0800 | [diff] [blame] | 96 | mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 97 | if (!mc9s) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | mc9s->chip.label = client->name; |
| 101 | mc9s->chip.base = -1; |
| 102 | mc9s->chip.dev = &client->dev; |
| 103 | mc9s->chip.owner = THIS_MODULE; |
| 104 | mc9s->chip.ngpio = GPIO_NUM; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 105 | mc9s->chip.can_sleep = true; |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 106 | mc9s->chip.get = mc9s08dz60_get_value; |
| 107 | mc9s->chip.set = mc9s08dz60_set_value; |
| 108 | mc9s->chip.direction_output = mc9s08dz60_direction_output; |
| 109 | mc9s->client = client; |
| 110 | i2c_set_clientdata(client, mc9s); |
| 111 | |
Axel Lin | 3c7051d | 2012-08-29 09:36:54 +0800 | [diff] [blame] | 112 | return gpiochip_add(&mc9s->chip); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int mc9s08dz60_remove(struct i2c_client *client) |
| 116 | { |
| 117 | struct mc9s08dz60 *mc9s; |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 118 | |
| 119 | mc9s = i2c_get_clientdata(client); |
| 120 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 121 | gpiochip_remove(&mc9s->chip); |
| 122 | return 0; |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static const struct i2c_device_id mc9s08dz60_id[] = { |
| 126 | {"mc9s08dz60", 0}, |
| 127 | {}, |
| 128 | }; |
| 129 | |
| 130 | MODULE_DEVICE_TABLE(i2c, mc9s08dz60_id); |
| 131 | |
| 132 | static struct i2c_driver mc9s08dz60_i2c_driver = { |
| 133 | .driver = { |
| 134 | .owner = THIS_MODULE, |
| 135 | .name = "mc9s08dz60", |
| 136 | }, |
| 137 | .probe = mc9s08dz60_probe, |
| 138 | .remove = mc9s08dz60_remove, |
| 139 | .id_table = mc9s08dz60_id, |
| 140 | }; |
| 141 | |
| 142 | module_i2c_driver(mc9s08dz60_i2c_driver); |
| 143 | |
| 144 | MODULE_AUTHOR("Freescale Semiconductor, Inc. " |
| 145 | "Wu Guoxing <b39297@freescale.com>"); |
| 146 | MODULE_DESCRIPTION("mc9s08dz60 gpio function on mx35 3ds board"); |
| 147 | MODULE_LICENSE("GPL v2"); |