1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Author: Wu Guoxing <b39297@freescale.com>
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)
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
)
33 struct mc9s08dz60
*mc9s
= gpiochip_get_data(gc
);
35 mc9s_gpio_to_reg_and_bit(offset
, ®
, &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
)
46 mc9s_gpio_to_reg_and_bit(offset
, ®
, &bit
);
47 value
= i2c_smbus_read_byte_data(mc9s
->client
, reg
);
54 return i2c_smbus_write_byte_data(mc9s
->client
, reg
, 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
);
85 mc9s
->chip
.label
= client
->name
;
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
[] = {
105 static struct i2c_driver mc9s08dz60_i2c_driver
= {
107 .name
= "mc9s08dz60",
109 .probe
= mc9s08dz60_probe
,
110 .id_table
= mc9s08dz60_id
,
112 builtin_i2c_driver(mc9s08dz60_i2c_driver
);