Blame view

drivers/gpio/gpio-mc9s08dz60.c 3.02 KB
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Copyright 2009-2012 Freescale Semiconductor, Inc. All Rights Reserved.
   *
   * Author: Wu Guoxing <b39297@freescale.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
  
  #include <linux/kernel.h>
0de6a80de   Paul Gortmaker   gpio: mc9s08dz60:...
18
  #include <linux/init.h>
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
19
20
21
22
23
24
25
26
27
28
29
30
  #include <linux/slab.h>
  #include <linux/i2c.h>
  #include <linux/gpio.h>
  
  #define GPIO_GROUP_NUM 2
  #define GPIO_NUM_PER_GROUP 8
  #define GPIO_NUM (GPIO_GROUP_NUM*GPIO_NUM_PER_GROUP)
  
  struct mc9s08dz60 {
  	struct i2c_client *client;
  	struct gpio_chip chip;
  };
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
31
32
33
34
35
36
37
38
39
40
  static void mc9s_gpio_to_reg_and_bit(int offset, u8 *reg, u8 *bit)
  {
  	*reg = 0x20 + offset / GPIO_NUM_PER_GROUP;
  	*bit = offset % GPIO_NUM_PER_GROUP;
  }
  
  static int mc9s08dz60_get_value(struct gpio_chip *gc, unsigned offset)
  {
  	u8 reg, bit;
  	s32 value;
cf90c9e35   Linus Walleij   gpio: mc9s08dz60:...
41
  	struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  
  	mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  	value = i2c_smbus_read_byte_data(mc9s->client, reg);
  
  	return (value >= 0) ? (value >> bit) & 0x1 : 0;
  }
  
  static int mc9s08dz60_set(struct mc9s08dz60 *mc9s, unsigned offset, int val)
  {
  	u8 reg, bit;
  	s32 value;
  
  	mc9s_gpio_to_reg_and_bit(offset, &reg, &bit);
  	value = i2c_smbus_read_byte_data(mc9s->client, reg);
  	if (value >= 0) {
  		if (val)
  			value |= 1 << bit;
  		else
  			value &= ~(1 << bit);
  
  		return i2c_smbus_write_byte_data(mc9s->client, reg, value);
  	} else
  		return value;
  
  }
  
  
  static void mc9s08dz60_set_value(struct gpio_chip *gc, unsigned offset, int val)
  {
cf90c9e35   Linus Walleij   gpio: mc9s08dz60:...
71
  	struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
72
73
74
75
76
77
78
  
  	mc9s08dz60_set(mc9s, offset, val);
  }
  
  static int mc9s08dz60_direction_output(struct gpio_chip *gc,
  				       unsigned offset, int val)
  {
cf90c9e35   Linus Walleij   gpio: mc9s08dz60:...
79
  	struct mc9s08dz60 *mc9s = gpiochip_get_data(gc);
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
80
81
82
83
84
85
86
  
  	return mc9s08dz60_set(mc9s, offset, val);
  }
  
  static int mc9s08dz60_probe(struct i2c_client *client,
  			    const struct i2c_device_id *id)
  {
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
87
  	struct mc9s08dz60 *mc9s;
3c7051d7c   Axel Lin   gpio: mc9s08dz60:...
88
  	mc9s = devm_kzalloc(&client->dev, sizeof(*mc9s), GFP_KERNEL);
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
89
90
91
92
93
  	if (!mc9s)
  		return -ENOMEM;
  
  	mc9s->chip.label = client->name;
  	mc9s->chip.base = -1;
58383c784   Linus Walleij   gpio: change memb...
94
  	mc9s->chip.parent = &client->dev;
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
95
96
  	mc9s->chip.owner = THIS_MODULE;
  	mc9s->chip.ngpio = GPIO_NUM;
9fb1f39eb   Linus Walleij   gpio/pinctrl: mak...
97
  	mc9s->chip.can_sleep = true;
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
98
99
100
101
102
  	mc9s->chip.get = mc9s08dz60_get_value;
  	mc9s->chip.set = mc9s08dz60_set_value;
  	mc9s->chip.direction_output = mc9s08dz60_direction_output;
  	mc9s->client = client;
  	i2c_set_clientdata(client, mc9s);
7d645df05   Laxman Dewangan   gpio: mc9s08dz60:...
103
  	return devm_gpiochip_add_data(&client->dev, &mc9s->chip, mc9s);
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
104
105
106
107
108
109
  }
  
  static const struct i2c_device_id mc9s08dz60_id[] = {
  	{"mc9s08dz60", 0},
  	{},
  };
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
110
111
  static struct i2c_driver mc9s08dz60_i2c_driver = {
  	.driver = {
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
112
113
114
  		.name = "mc9s08dz60",
  	},
  	.probe = mc9s08dz60_probe,
608589b15   wu guoxing   ARM/mx35/3ds: gpi...
115
116
  	.id_table = mc9s08dz60_id,
  };
0de6a80de   Paul Gortmaker   gpio: mc9s08dz60:...
117
  builtin_i2c_driver(mc9s08dz60_i2c_driver);