Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Wu Guoxing <b39297@freescale.com> |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Paul Gortmaker | 0de6a80 | 2016-03-27 11:44:43 -0400 | [diff] [blame] | 9 | #include <linux/init.h> |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/i2c.h> |
Linus Walleij | 3dc1c92 | 2018-04-13 15:11:30 +0200 | [diff] [blame] | 12 | #include <linux/gpio/driver.h> |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 13 | |
| 14 | #define GPIO_GROUP_NUM 2 |
| 15 | #define GPIO_NUM_PER_GROUP 8 |
| 16 | #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP) |
| 17 | |
| 18 | struct mc9s08dz60 { |
| 19 | struct i2c_client *client; |
| 20 | struct gpio_chip chip; |
| 21 | }; |
| 22 | |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 23 | static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit) |
| 24 | { |
| 25 | *reg = 0x20 + offset / GPIO_NUM_PER_GROUP; |
| 26 | *bit = offset % GPIO_NUM_PER_GROUP; |
| 27 | } |
| 28 | |
| 29 | static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset) |
| 30 | { |
| 31 | u8 reg, bit; |
| 32 | s32 value; |
Linus Walleij | cf90c9e | 2015-12-07 10:07:07 +0100 | [diff] [blame] | 33 | struct mc9s08dz60 *mc9s = gpiochip_get_data(gc); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 34 | |
| 35 | mc9s_gpio_to_reg_and_bit(offset, ®, &bit); |
| 36 | value = i2c_smbus_read_byte_data(mc9s->client, reg); |
| 37 | |
| 38 | return (value >= 0) ? (value >> bit) & 0x1 : 0; |
| 39 | } |
| 40 | |
| 41 | static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val) |
| 42 | { |
| 43 | u8 reg, bit; |
| 44 | s32 value; |
| 45 | |
| 46 | mc9s_gpio_to_reg_and_bit(offset, ®, &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 | |
| 54 | return i2c_smbus_write_byte_data(mc9s->client, reg, value); |
| 55 | } else |
| 56 | return value; |
| 57 | |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val) |
| 62 | { |
Linus Walleij | cf90c9e | 2015-12-07 10:07:07 +0100 | [diff] [blame] | 63 | struct mc9s08dz60 *mc9s = gpiochip_get_data(gc); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 64 | |
| 65 | mc9s08dz60_set(mc9s, offset, val); |
| 66 | } |
| 67 | |
| 68 | static int mc9s08dz60_direction_output(struct gpio_chip *gc, |
| 69 | unsigned offset, int val) |
| 70 | { |
Linus Walleij | cf90c9e | 2015-12-07 10:07:07 +0100 | [diff] [blame] | 71 | struct mc9s08dz60 *mc9s = gpiochip_get_data(gc); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 72 | |
| 73 | return mc9s08dz60_set(mc9s, offset, val); |
| 74 | } |
| 75 | |
| 76 | static int mc9s08dz60_probe(struct i2c_client *client, |
| 77 | const struct i2c_device_id *id) |
| 78 | { |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 79 | struct mc9s08dz60 *mc9s; |
| 80 | |
Axel Lin | 3c7051d | 2012-08-29 09:36:54 +0800 | [diff] [blame] | 81 | mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 82 | if (!mc9s) |
| 83 | return -ENOMEM; |
| 84 | |
| 85 | mc9s->chip.label = client->name; |
| 86 | mc9s->chip.base = -1; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 87 | mc9s->chip.parent = &client->dev; |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 88 | mc9s->chip.owner = THIS_MODULE; |
| 89 | mc9s->chip.ngpio = GPIO_NUM; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 90 | mc9s->chip.can_sleep = true; |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 91 | mc9s->chip.get = mc9s08dz60_get_value; |
| 92 | mc9s->chip.set = mc9s08dz60_set_value; |
| 93 | mc9s->chip.direction_output = mc9s08dz60_direction_output; |
| 94 | mc9s->client = client; |
| 95 | i2c_set_clientdata(client, mc9s); |
| 96 | |
Laxman Dewangan | 7d645df | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 97 | return devm_gpiochip_add_data(&client->dev, &mc9s->chip, mc9s); |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static const struct i2c_device_id mc9s08dz60_id[] = { |
| 101 | {"mc9s08dz60", 0}, |
| 102 | {}, |
| 103 | }; |
| 104 | |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 105 | static struct i2c_driver mc9s08dz60_i2c_driver = { |
| 106 | .driver = { |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 107 | .name = "mc9s08dz60", |
| 108 | }, |
| 109 | .probe = mc9s08dz60_probe, |
wu guoxing | 608589b | 2012-01-30 14:31:20 +0800 | [diff] [blame] | 110 | .id_table = mc9s08dz60_id, |
| 111 | }; |
Paul Gortmaker | 0de6a80 | 2016-03-27 11:44:43 -0400 | [diff] [blame] | 112 | builtin_i2c_driver(mc9s08dz60_i2c_driver); |