Skip to content

Commit f0cfec1

Browse files
hongliu68torvalds
authored andcommitted
drivers/misc/apds9802als.c: add runtime PM support
Update the driver for the needed runtime power features. Remove the old user controlled power functions. [akpm@linux-foundation.org: put PM code under CONFIG_PM] Signed-off-by: Hong Liu <hong.liu@intel.com> Signed-off-by: Alan Cox <alan@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 22d96aa commit f0cfec1

1 file changed

Lines changed: 103 additions & 64 deletions

File tree

‎drivers/misc/apds9802als.c‎

Lines changed: 103 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@
2929
#include <linux/delay.h>
3030
#include <linux/mutex.h>
3131
#include <linux/sysfs.h>
32-
#include <linux/hwmon-sysfs.h>
32+
#include <linux/pm_runtime.h>
3333

3434
#define ALS_MIN_RANGE_VAL 1
3535
#define ALS_MAX_RANGE_VAL 2
3636
#define POWER_STA_ENABLE 1
3737
#define POWER_STA_DISABLE 0
38-
#define APDS9802ALS_I2C_ADDR 0x29
3938

4039
#define DRIVER_NAME "apds9802als"
4140

4241
struct als_data {
43-
struct device *hwmon_dev;
44-
bool needresume;
4542
struct mutex mutex;
4643
};
4744

@@ -60,29 +57,64 @@ static ssize_t als_sensing_range_show(struct device *dev,
6057
return sprintf(buf, "65535\n");
6158
}
6259

60+
static int als_wait_for_data_ready(struct device *dev)
61+
{
62+
struct i2c_client *client = to_i2c_client(dev);
63+
int ret;
64+
int retry = 10;
65+
66+
do {
67+
msleep(30);
68+
ret = i2c_smbus_read_byte_data(client, 0x86);
69+
} while (!(ret & 0x80) && retry--);
70+
71+
if (!retry) {
72+
dev_warn(dev, "timeout waiting for data ready\n");
73+
return -ETIMEDOUT;
74+
}
75+
76+
return 0;
77+
}
78+
6379
static ssize_t als_lux0_input_data_show(struct device *dev,
6480
struct device_attribute *attr, char *buf)
6581
{
6682
struct i2c_client *client = to_i2c_client(dev);
6783
struct als_data *data = i2c_get_clientdata(client);
68-
unsigned int ret_val;
84+
int ret_val;
6985
int temp;
7086

7187
/* Protect against parallel reads */
88+
pm_runtime_get_sync(dev);
7289
mutex_lock(&data->mutex);
73-
temp = i2c_smbus_read_byte_data(client, 0x8C);/*LSB data*/
90+
91+
/* clear EOC interrupt status */
92+
i2c_smbus_write_byte(client, 0x40);
93+
/* start measurement */
94+
temp = i2c_smbus_read_byte_data(client, 0x81);
95+
i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
96+
97+
ret_val = als_wait_for_data_ready(dev);
98+
if (ret_val < 0)
99+
goto failed;
100+
101+
temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
74102
if (temp < 0) {
75103
ret_val = temp;
76104
goto failed;
77105
}
78-
ret_val = i2c_smbus_read_byte_data(client, 0x8D);/*MSB data*/
106+
ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
79107
if (ret_val < 0)
80108
goto failed;
109+
81110
mutex_unlock(&data->mutex);
82-
ret_val = (ret_val << 8) | temp;
83-
return sprintf(buf, "%d\n", ret_val);
111+
pm_runtime_put_sync(dev);
112+
113+
temp = (ret_val << 8) | temp;
114+
return sprintf(buf, "%d\n", temp);
84115
failed:
85116
mutex_unlock(&data->mutex);
117+
pm_runtime_put_sync(dev);
86118
return ret_val;
87119
}
88120

@@ -104,9 +136,10 @@ static ssize_t als_sensing_range_store(struct device *dev,
104136
else
105137
return -ERANGE;
106138

139+
pm_runtime_get_sync(dev);
140+
107141
/* Make sure nobody else reads/modifies/writes 0x81 while we
108142
are active */
109-
110143
mutex_lock(&data->mutex);
111144

112145
ret_val = i2c_smbus_read_byte_data(client, 0x81);
@@ -116,34 +149,25 @@ static ssize_t als_sensing_range_store(struct device *dev,
116149
/* Reset the bits before setting them */
117150
ret_val = ret_val & 0xFA;
118151

119-
if (val == 1) /* Setting the continous measurement up to 4k LUX */
120-
ret_val = (ret_val | 0x05);
121-
else /* Setting the continous measurement up to 64k LUX*/
122-
ret_val = (ret_val | 0x04);
152+
if (val == 1) /* Setting detection range up to 4k LUX */
153+
ret_val = (ret_val | 0x01);
154+
else /* Setting detection range up to 64k LUX*/
155+
ret_val = (ret_val | 0x00);
123156

124157
ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
158+
125159
if (ret_val >= 0) {
126160
/* All OK */
127161
mutex_unlock(&data->mutex);
162+
pm_runtime_put_sync(dev);
128163
return count;
129164
}
130165
fail:
131166
mutex_unlock(&data->mutex);
167+
pm_runtime_put_sync(dev);
132168
return ret_val;
133169
}
134170

135-
static ssize_t als_power_status_show(struct device *dev,
136-
struct device_attribute *attr, char *buf)
137-
{
138-
struct i2c_client *client = to_i2c_client(dev);
139-
int ret_val;
140-
ret_val = i2c_smbus_read_byte_data(client, 0x80);
141-
if (ret_val < 0)
142-
return ret_val;
143-
ret_val = ret_val & 0x01;
144-
return sprintf(buf, "%d\n", ret_val);
145-
}
146-
147171
static int als_set_power_state(struct i2c_client *client, bool on_off)
148172
{
149173
int ret_val;
@@ -163,39 +187,13 @@ static int als_set_power_state(struct i2c_client *client, bool on_off)
163187
return ret_val;
164188
}
165189

166-
static ssize_t als_power_status_store(struct device *dev,
167-
struct device_attribute *attr, const char *buf, size_t count)
168-
{
169-
struct i2c_client *client = to_i2c_client(dev);
170-
struct als_data *data = i2c_get_clientdata(client);
171-
unsigned long val;
172-
int ret_val;
173-
174-
if (strict_strtoul(buf, 10, &val))
175-
return -EINVAL;
176-
if (val == POWER_STA_ENABLE) {
177-
ret_val = als_set_power_state(client, true);
178-
data->needresume = true;
179-
} else if (val == POWER_STA_DISABLE) {
180-
ret_val = als_set_power_state(client, false);
181-
data->needresume = false;
182-
} else
183-
return -EINVAL;
184-
if (ret_val < 0)
185-
return ret_val;
186-
return count;
187-
}
188-
189190
static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
190191
als_sensing_range_show, als_sensing_range_store);
191192
static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
192-
static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
193-
als_power_status_show, als_power_status_store);
194193

195194
static struct attribute *mid_att_als[] = {
196195
&dev_attr_lux0_sensor_range.attr,
197196
&dev_attr_lux0_input.attr,
198-
&dev_attr_power_state.attr,
199197
NULL
200198
};
201199

@@ -213,15 +211,21 @@ static int als_set_default_config(struct i2c_client *client)
213211
dev_err(&client->dev, "failed default switch on write\n");
214212
return ret_val;
215213
}
216-
/* Continous from 1Lux to 64k Lux */
217-
ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x04);
214+
/* detection range: 1~64K Lux, maunal measurement */
215+
ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
218216
if (ret_val < 0)
219217
dev_err(&client->dev, "failed default LUX on write\n");
218+
219+
/* We always get 0 for the 1st measurement after system power on,
220+
* so make sure it is finished before user asks for data.
221+
*/
222+
als_wait_for_data_ready(&client->dev);
223+
220224
return ret_val;
221225
}
222226

223-
static int apds9802als_probe(struct i2c_client *client,
224-
const struct i2c_device_id *id)
227+
static int apds9802als_probe(struct i2c_client *client,
228+
const struct i2c_device_id *id)
225229
{
226230
int res;
227231
struct als_data *data;
@@ -237,11 +241,14 @@ static int apds9802als_probe(struct i2c_client *client,
237241
dev_err(&client->dev, "device create file failed\n");
238242
goto als_error1;
239243
}
240-
dev_info(&client->dev,
241-
"%s apds9802als: ALS chip found\n", client->name);
244+
dev_info(&client->dev, "ALS chip found\n");
242245
als_set_default_config(client);
243-
data->needresume = true;
244246
mutex_init(&data->mutex);
247+
248+
pm_runtime_enable(&client->dev);
249+
pm_runtime_get(&client->dev);
250+
pm_runtime_put(&client->dev);
251+
245252
return res;
246253
als_error1:
247254
i2c_set_clientdata(client, NULL);
@@ -252,11 +259,14 @@ static int apds9802als_probe(struct i2c_client *client,
252259
static int apds9802als_remove(struct i2c_client *client)
253260
{
254261
struct als_data *data = i2c_get_clientdata(client);
262+
263+
als_set_power_state(client, false);
255264
sysfs_remove_group(&client->dev.kobj, &m_als_gr);
256265
kfree(data);
257266
return 0;
258267
}
259268

269+
#ifdef CONFIG_PM
260270
static int apds9802als_suspend(struct i2c_client *client, pm_message_t mesg)
261271
{
262272
als_set_power_state(client, false);
@@ -265,13 +275,42 @@ static int apds9802als_suspend(struct i2c_client *client, pm_message_t mesg)
265275

266276
static int apds9802als_resume(struct i2c_client *client)
267277
{
268-
struct als_data *data = i2c_get_clientdata(client);
278+
als_set_default_config(client);
269279

270-
if (data->needresume == true)
271-
als_set_power_state(client, true);
280+
pm_runtime_get(&client->dev);
281+
pm_runtime_put(&client->dev);
272282
return 0;
273283
}
274284

285+
static int apds9802als_runtime_suspend(struct device *dev)
286+
{
287+
struct i2c_client *client = to_i2c_client(dev);
288+
289+
als_set_power_state(client, false);
290+
return 0;
291+
}
292+
293+
static int apds9802als_runtime_resume(struct device *dev)
294+
{
295+
struct i2c_client *client = to_i2c_client(dev);
296+
297+
als_set_power_state(client, true);
298+
return 0;
299+
}
300+
301+
static const struct dev_pm_ops apds9802als_pm_ops = {
302+
.runtime_suspend = apds9802als_runtime_suspend,
303+
.runtime_resume = apds9802als_runtime_resume,
304+
};
305+
306+
#define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
307+
308+
#else /* CONFIG_PM */
309+
#define apds9802als_suspend NULL
310+
#define apds9802als_resume NULL
311+
#define APDS9802ALS_PM_OPS NULL
312+
#endif /* CONFIG_PM */
313+
275314
static struct i2c_device_id apds9802als_id[] = {
276315
{ DRIVER_NAME, 0 },
277316
{ }
@@ -281,8 +320,8 @@ MODULE_DEVICE_TABLE(i2c, apds9802als_id);
281320

282321
static struct i2c_driver apds9802als_driver = {
283322
.driver = {
284-
.name = DRIVER_NAME,
285-
.owner = THIS_MODULE,
323+
.name = DRIVER_NAME,
324+
.pm = APDS9802ALS_PM_OPS,
286325
},
287326
.probe = apds9802als_probe,
288327
.remove = apds9802als_remove,

0 commit comments

Comments
 (0)