Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sec-core.c
Go to the documentation of this file.
1 /*
2  * sec-core.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd
5  * http://www.samsung.com
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/mutex.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/samsung/core.h>
25 #include <linux/mfd/samsung/irq.h>
26 #include <linux/mfd/samsung/rtc.h>
27 #include <linux/regmap.h>
28 
29 static struct mfd_cell s5m8751_devs[] = {
30  {
31  .name = "s5m8751-pmic",
32  }, {
33  .name = "s5m-charger",
34  }, {
35  .name = "s5m8751-codec",
36  },
37 };
38 
39 static struct mfd_cell s5m8763_devs[] = {
40  {
41  .name = "s5m8763-pmic",
42  }, {
43  .name = "s5m-rtc",
44  }, {
45  .name = "s5m-charger",
46  },
47 };
48 
49 static struct mfd_cell s5m8767_devs[] = {
50  {
51  .name = "s5m8767-pmic",
52  }, {
53  .name = "s5m-rtc",
54  },
55 };
56 
57 static struct mfd_cell s2mps11_devs[] = {
58  {
59  .name = "s2mps11-pmic",
60  },
61 };
62 
63 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
64 {
65  return regmap_read(sec_pmic->regmap, reg, dest);
66 }
68 
69 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
70 {
71  return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
72 }
74 
75 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
76 {
77  return regmap_write(sec_pmic->regmap, reg, value);
78 }
80 
81 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
82 {
83  return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
84 }
86 
87 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
88 {
89  return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
90 }
92 
93 static struct regmap_config sec_regmap_config = {
94  .reg_bits = 8,
95  .val_bits = 8,
96 };
97 
98 static int sec_pmic_probe(struct i2c_client *i2c,
99  const struct i2c_device_id *id)
100 {
101  struct sec_platform_data *pdata = i2c->dev.platform_data;
102  struct sec_pmic_dev *sec_pmic;
103  int ret;
104 
105  sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
106  GFP_KERNEL);
107  if (sec_pmic == NULL)
108  return -ENOMEM;
109 
110  i2c_set_clientdata(i2c, sec_pmic);
111  sec_pmic->dev = &i2c->dev;
112  sec_pmic->i2c = i2c;
113  sec_pmic->irq = i2c->irq;
114  sec_pmic->type = id->driver_data;
115 
116  if (pdata) {
117  sec_pmic->device_type = pdata->device_type;
118  sec_pmic->ono = pdata->ono;
119  sec_pmic->irq_base = pdata->irq_base;
120  sec_pmic->wakeup = pdata->wakeup;
121  }
122 
123  sec_pmic->regmap = devm_regmap_init_i2c(i2c, &sec_regmap_config);
124  if (IS_ERR(sec_pmic->regmap)) {
125  ret = PTR_ERR(sec_pmic->regmap);
126  dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
127  ret);
128  return ret;
129  }
130 
131  sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
132  i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
133 
134  if (pdata && pdata->cfg_pmic_irq)
135  pdata->cfg_pmic_irq();
136 
137  sec_irq_init(sec_pmic);
138 
139  pm_runtime_set_active(sec_pmic->dev);
140 
141  switch (sec_pmic->device_type) {
142  case S5M8751X:
143  ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
144  ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
145  break;
146  case S5M8763X:
147  ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
148  ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
149  break;
150  case S5M8767X:
151  ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
152  ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
153  break;
154  case S2MPS11X:
155  ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
156  ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
157  break;
158  default:
159  /* If this happens the probe function is problem */
160  BUG();
161  }
162 
163  if (ret < 0)
164  goto err;
165 
166  return ret;
167 
168 err:
169  mfd_remove_devices(sec_pmic->dev);
170  sec_irq_exit(sec_pmic);
171  i2c_unregister_device(sec_pmic->rtc);
172  return ret;
173 }
174 
175 static int sec_pmic_remove(struct i2c_client *i2c)
176 {
177  struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
178 
179  mfd_remove_devices(sec_pmic->dev);
180  sec_irq_exit(sec_pmic);
181  i2c_unregister_device(sec_pmic->rtc);
182  return 0;
183 }
184 
185 static const struct i2c_device_id sec_pmic_id[] = {
186  { "sec_pmic", 0 },
187  { }
188 };
189 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
190 
191 static struct i2c_driver sec_pmic_driver = {
192  .driver = {
193  .name = "sec_pmic",
194  .owner = THIS_MODULE,
195  },
196  .probe = sec_pmic_probe,
197  .remove = sec_pmic_remove,
198  .id_table = sec_pmic_id,
199 };
200 
201 static int __init sec_pmic_init(void)
202 {
203  return i2c_add_driver(&sec_pmic_driver);
204 }
205 
206 subsys_initcall(sec_pmic_init);
207 
208 static void __exit sec_pmic_exit(void)
209 {
210  i2c_del_driver(&sec_pmic_driver);
211 }
212 module_exit(sec_pmic_exit);
213 
214 MODULE_AUTHOR("Sangbeom Kim <[email protected]>");
215 MODULE_DESCRIPTION("Core support for the S5M MFD");
216 MODULE_LICENSE("GPL");