Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpio / gpio-mc9s08dz60.c
bloba9f17cebd5edd84188414b9423417922444f22c8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Author: Wu Guoxing <b39297@freescale.com>
6 */
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/gpio/driver.h>
14 #define GPIO_GROUP_NUM 2
15 #define GPIO_NUM_PER_GROUP 8
16 #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP)
18 struct mc9s08dz60 {
19 struct i2c_client *client;
20 struct gpio_chip chip;
23 static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit)
25 *reg = 0x20 + offset / GPIO_NUM_PER_GROUP;
26 *bit = offset % GPIO_NUM_PER_GROUP;
29 static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset)
31 u8 reg, bit;
32 s32 value;
33 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
35 mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
36 value = i2c_smbus_read_byte_data(mc9s->client, reg);
38 return (value >= 0) ? (value >> bit) & 0x1 : 0;
41 static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val)
43 u8 reg, bit;
44 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);
54 return i2c_smbus_write_byte_data(mc9s->client, reg, value);
55 } else
56 return value;
61 static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val)
63 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
65 mc9s08dz60_set(mc9s, offset, val);
68 static int mc9s08dz60_direction_output(struct gpio_chip *gc,
69 unsigned offset, int val)
71 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
73 return mc9s08dz60_set(mc9s, offset, val);
76 static int mc9s08dz60_probe(struct i2c_client *client,
77 const struct i2c_device_id *id)
79 struct mc9s08dz60 *mc9s;
81 mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL);
82 if (!mc9s)
83 return -ENOMEM;
85 mc9s->chip.label = client->name;
86 mc9s->chip.base = -1;
87 mc9s->chip.parent = &client->dev;
88 mc9s->chip.owner = THIS_MODULE;
89 mc9s->chip.ngpio = GPIO_NUM;
90 mc9s->chip.can_sleep = true;
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);
97 return devm_gpiochip_add_data(&client->dev, &mc9s->chip, mc9s);
100 static const struct i2c_device_id mc9s08dz60_id[] = {
101 {"mc9s08dz60", 0},
105 static struct i2c_driver mc9s08dz60_i2c_driver = {
106 .driver = {
107 .name = "mc9s08dz60",
109 .probe = mc9s08dz60_probe,
110 .id_table = mc9s08dz60_id,
112 builtin_i2c_driver(mc9s08dz60_i2c_driver);