blob: ba22fb92a6e7df10d411ccd9882677fae80bf465 [file] [log] [blame]
wu guoxing608589b2012-01-30 14:31:20 +08001/*
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
27struct mc9s08dz60 {
28 struct i2c_client *client;
29 struct gpio_chip chip;
30};
31
wu guoxing608589b2012-01-30 14:31:20 +080032static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit)
33{
34 *reg = 0x20 + offset / GPIO_NUM_PER_GROUP;
35 *bit = offset % GPIO_NUM_PER_GROUP;
36}
37
38static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset)
39{
40 u8 reg, bit;
41 s32 value;
Linus Walleijcf90c9e2015-12-07 10:07:07 +010042 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
wu guoxing608589b2012-01-30 14:31:20 +080043
44 mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
45 value = i2c_smbus_read_byte_data(mc9s->client, reg);
46
47 return (value >= 0) ? (value >> bit) & 0x1 : 0;
48}
49
50static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val)
51{
52 u8 reg, bit;
53 s32 value;
54
55 mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
56 value = i2c_smbus_read_byte_data(mc9s->client, reg);
57 if (value >= 0) {
58 if (val)
59 value |= 1 << bit;
60 else
61 value &= ~(1 << bit);
62
63 return i2c_smbus_write_byte_data(mc9s->client, reg, value);
64 } else
65 return value;
66
67}
68
69
70static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val)
71{
Linus Walleijcf90c9e2015-12-07 10:07:07 +010072 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
wu guoxing608589b2012-01-30 14:31:20 +080073
74 mc9s08dz60_set(mc9s, offset, val);
75}
76
77static int mc9s08dz60_direction_output(struct gpio_chip *gc,
78 unsigned offset, int val)
79{
Linus Walleijcf90c9e2015-12-07 10:07:07 +010080 struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
wu guoxing608589b2012-01-30 14:31:20 +080081
82 return mc9s08dz60_set(mc9s, offset, val);
83}
84
85static int mc9s08dz60_probe(struct i2c_client *client,
86 const struct i2c_device_id *id)
87{
wu guoxing608589b2012-01-30 14:31:20 +080088 struct mc9s08dz60 *mc9s;
89
Axel Lin3c7051d2012-08-29 09:36:54 +080090 mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL);
wu guoxing608589b2012-01-30 14:31:20 +080091 if (!mc9s)
92 return -ENOMEM;
93
94 mc9s->chip.label = client->name;
95 mc9s->chip.base = -1;
Linus Walleij58383c72015-11-04 09:56:26 +010096 mc9s->chip.parent = &client->dev;
wu guoxing608589b2012-01-30 14:31:20 +080097 mc9s->chip.owner = THIS_MODULE;
98 mc9s->chip.ngpio = GPIO_NUM;
Linus Walleij9fb1f392013-12-04 14:42:46 +010099 mc9s->chip.can_sleep = true;
wu guoxing608589b2012-01-30 14:31:20 +0800100 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);
105
Linus Walleijcf90c9e2015-12-07 10:07:07 +0100106 return gpiochip_add_data(&mc9s->chip, mc9s);
wu guoxing608589b2012-01-30 14:31:20 +0800107}
108
109static int mc9s08dz60_remove(struct i2c_client *client)
110{
111 struct mc9s08dz60 *mc9s;
wu guoxing608589b2012-01-30 14:31:20 +0800112
113 mc9s = i2c_get_clientdata(client);
114
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200115 gpiochip_remove(&mc9s->chip);
116 return 0;
wu guoxing608589b2012-01-30 14:31:20 +0800117}
118
119static const struct i2c_device_id mc9s08dz60_id[] = {
120 {"mc9s08dz60", 0},
121 {},
122};
123
124MODULE_DEVICE_TABLE(i2c, mc9s08dz60_id);
125
126static struct i2c_driver mc9s08dz60_i2c_driver = {
127 .driver = {
wu guoxing608589b2012-01-30 14:31:20 +0800128 .name = "mc9s08dz60",
129 },
130 .probe = mc9s08dz60_probe,
131 .remove = mc9s08dz60_remove,
132 .id_table = mc9s08dz60_id,
133};
134
135module_i2c_driver(mc9s08dz60_i2c_driver);
136
137MODULE_AUTHOR("Freescale Semiconductor, Inc. "
138 "Wu Guoxing <b39297@freescale.com>");
139MODULE_DESCRIPTION("mc9s08dz60 gpio function on mx35 3ds board");
140MODULE_LICENSE("GPL v2");