2 * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Author: Wu Guoxing <b39297@freescale.com>
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/i2c.h>
21 #include <linux/gpio/driver.h>
23 #define GPIO_GROUP_NUM 2
24 #define GPIO_NUM_PER_GROUP 8
25 #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP)
28 struct i2c_client
*client
;
29 struct gpio_chip chip
;
32 static void mc9s_gpio_to_reg_and_bit(int offset
, u8
*reg
, u8
*bit
)
34 *reg
= 0x20 + offset
/ GPIO_NUM_PER_GROUP
;
35 *bit
= offset
% GPIO_NUM_PER_GROUP
;
38 static int mc9s08dz60_get_value(struct gpio_chip
*gc
, unsigned offset
)
42 struct mc9s08dz60
*mc9s
= gpiochip_get_data(gc
);
44 mc9s_gpio_to_reg_and_bit(offset
, ®
, &bit
);
45 value
= i2c_smbus_read_byte_data(mc9s
->client
, reg
);
47 return (value
>= 0) ? (value
>> bit
) & 0x1 : 0;
50 static int mc9s08dz60_set(struct mc9s08dz60
*mc9s
, unsigned offset
, int val
)
55 mc9s_gpio_to_reg_and_bit(offset
, ®
, &bit
);
56 value
= i2c_smbus_read_byte_data(mc9s
->client
, reg
);
63 return i2c_smbus_write_byte_data(mc9s
->client
, reg
, value
);
70 static void mc9s08dz60_set_value(struct gpio_chip
*gc
, unsigned offset
, int val
)
72 struct mc9s08dz60
*mc9s
= gpiochip_get_data(gc
);
74 mc9s08dz60_set(mc9s
, offset
, val
);
77 static int mc9s08dz60_direction_output(struct gpio_chip
*gc
,
78 unsigned offset
, int val
)
80 struct mc9s08dz60
*mc9s
= gpiochip_get_data(gc
);
82 return mc9s08dz60_set(mc9s
, offset
, val
);
85 static int mc9s08dz60_probe(struct i2c_client
*client
,
86 const struct i2c_device_id
*id
)
88 struct mc9s08dz60
*mc9s
;
90 mc9s
= devm_kzalloc(&client
->dev
, sizeof(*mc9s
), GFP_KERNEL
);
94 mc9s
->chip
.label
= client
->name
;
96 mc9s
->chip
.parent
= &client
->dev
;
97 mc9s
->chip
.owner
= THIS_MODULE
;
98 mc9s
->chip
.ngpio
= GPIO_NUM
;
99 mc9s
->chip
.can_sleep
= true;
100 mc9s
->chip
.get
= mc9s08dz60_get_value
;
101 mc9s
->chip
.set
= mc9s08dz60_set_value
;
102 mc9s
->chip
.direction_output
= mc9s08dz60_direction_output
;
103 mc9s
->client
= client
;
104 i2c_set_clientdata(client
, mc9s
);
106 return devm_gpiochip_add_data(&client
->dev
, &mc9s
->chip
, mc9s
);
109 static const struct i2c_device_id mc9s08dz60_id
[] = {
114 static struct i2c_driver mc9s08dz60_i2c_driver
= {
116 .name
= "mc9s08dz60",
118 .probe
= mc9s08dz60_probe
,
119 .id_table
= mc9s08dz60_id
,
121 builtin_i2c_driver(mc9s08dz60_i2c_driver
);