diff options
author | Daniel Baluta <daniel.baluta@intel.com> | 2015-04-21 12:10:59 -0400 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2015-04-26 09:53:14 -0400 |
commit | 8592a7eefa540303dd9e60fa49340d09ca9376b4 (patch) | |
tree | 12ea1725844ba0141f3fc368893d9ce002a497d9 | |
parent | 772154d0ddde6b46a3866c73a16cfbfaf3053be4 (diff) |
iio: ltr501: Add support for ltr559 chip
This device is register compatible with LTR501, with a minor difference for
ALS control register as showed below:
ALS Control register for LTR501:
7 6 5 4 3 2 1 0
+------+------+------+------+------+------+------+------+
| | | | |
| Reserved | Gain | SW | ALS Mode |
| | | Reset| |
+------+------+------+------+------+------+------+------+
ALS Control register for LTR559:
7 6 5 4 3 2 1 0
+------+------+------+------+------+------+------+------+
| | | | |
| Reserved | Gain | SW | ALS |
| | | Reset| Mode |
+------+------+------+------+------+------+------+------+
We handle this difference by introducing ltr501_chip_info.
Datasheet for LTR559 is at:
http://optoelectronics.liteon.com/upload/download/DS86-2013-0003/S_110_LTR-559ALS-01_DS_V1.pdf
Signed-off-by: Daniel Baluta <daniel.baluta@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r-- | drivers/iio/light/Kconfig | 3 | ||||
-rw-r--r-- | drivers/iio/light/ltr501.c | 218 |
2 files changed, 186 insertions, 35 deletions
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index 01a1a16ab7be..16a0ba11ab6e 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig | |||
@@ -169,7 +169,8 @@ config LTR501 | |||
169 | select IIO_TRIGGERED_BUFFER | 169 | select IIO_TRIGGERED_BUFFER |
170 | help | 170 | help |
171 | If you say yes here you get support for the Lite-On LTR-501ALS-01 | 171 | If you say yes here you get support for the Lite-On LTR-501ALS-01 |
172 | ambient light and proximity sensor. | 172 | ambient light and proximity sensor. This driver also supports LTR-559 |
173 | ALS/PS sensor. | ||
173 | 174 | ||
174 | This driver can also be built as a module. If so, the module | 175 | This driver can also be built as a module. If so, the module |
175 | will be called ltr501. | 176 | will be called ltr501. |
diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c index 0162e8652118..92da5146bc1c 100644 --- a/drivers/iio/light/ltr501.c +++ b/drivers/iio/light/ltr501.c | |||
@@ -88,9 +88,63 @@ struct ltr501_samp_table { | |||
88 | int time_val; /* repetition rate in micro seconds */ | 88 | int time_val; /* repetition rate in micro seconds */ |
89 | }; | 89 | }; |
90 | 90 | ||
91 | #define LTR501_RESERVED_GAIN -1 | ||
92 | |||
93 | enum { | ||
94 | ltr501 = 0, | ||
95 | ltr559, | ||
96 | }; | ||
97 | |||
98 | struct ltr501_gain { | ||
99 | int scale; | ||
100 | int uscale; | ||
101 | }; | ||
102 | |||
103 | static struct ltr501_gain ltr501_als_gain_tbl[] = { | ||
104 | {1, 0}, | ||
105 | {0, 5000}, | ||
106 | }; | ||
107 | |||
108 | static struct ltr501_gain ltr559_als_gain_tbl[] = { | ||
109 | {1, 0}, | ||
110 | {0, 500000}, | ||
111 | {0, 250000}, | ||
112 | {0, 125000}, | ||
113 | {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, | ||
114 | {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, | ||
115 | {0, 20000}, | ||
116 | {0, 10000}, | ||
117 | }; | ||
118 | |||
119 | static struct ltr501_gain ltr501_ps_gain_tbl[] = { | ||
120 | {1, 0}, | ||
121 | {0, 250000}, | ||
122 | {0, 125000}, | ||
123 | {0, 62500}, | ||
124 | }; | ||
125 | |||
126 | static struct ltr501_gain ltr559_ps_gain_tbl[] = { | ||
127 | {0, 62500}, /* x16 gain */ | ||
128 | {0, 31250}, /* x32 gain */ | ||
129 | {0, 15625}, /* bits X1 are for x64 gain */ | ||
130 | {0, 15624}, | ||
131 | }; | ||
132 | |||
133 | struct ltr501_chip_info { | ||
134 | u8 partid; | ||
135 | struct ltr501_gain *als_gain; | ||
136 | int als_gain_tbl_size; | ||
137 | struct ltr501_gain *ps_gain; | ||
138 | int ps_gain_tbl_size; | ||
139 | u8 als_mode_active; | ||
140 | u8 als_gain_mask; | ||
141 | u8 als_gain_shift; | ||
142 | }; | ||
143 | |||
91 | struct ltr501_data { | 144 | struct ltr501_data { |
92 | struct i2c_client *client; | 145 | struct i2c_client *client; |
93 | struct mutex lock_als, lock_ps; | 146 | struct mutex lock_als, lock_ps; |
147 | struct ltr501_chip_info *chip_info; | ||
94 | u8 als_contr, ps_contr; | 148 | u8 als_contr, ps_contr; |
95 | int als_period, ps_period; /* period in micro seconds */ | 149 | int als_period, ps_period; /* period in micro seconds */ |
96 | struct regmap *regmap; | 150 | struct regmap *regmap; |
@@ -516,10 +570,6 @@ static const struct iio_chan_spec ltr501_channels[] = { | |||
516 | IIO_CHAN_SOFT_TIMESTAMP(3), | 570 | IIO_CHAN_SOFT_TIMESTAMP(3), |
517 | }; | 571 | }; |
518 | 572 | ||
519 | static const int ltr501_ps_gain[4][2] = { | ||
520 | {1, 0}, {0, 250000}, {0, 125000}, {0, 62500} | ||
521 | }; | ||
522 | |||
523 | static int ltr501_read_raw(struct iio_dev *indio_dev, | 573 | static int ltr501_read_raw(struct iio_dev *indio_dev, |
524 | struct iio_chan_spec const *chan, | 574 | struct iio_chan_spec const *chan, |
525 | int *val, int *val2, long mask) | 575 | int *val, int *val2, long mask) |
@@ -557,19 +607,16 @@ static int ltr501_read_raw(struct iio_dev *indio_dev, | |||
557 | case IIO_CHAN_INFO_SCALE: | 607 | case IIO_CHAN_INFO_SCALE: |
558 | switch (chan->type) { | 608 | switch (chan->type) { |
559 | case IIO_INTENSITY: | 609 | case IIO_INTENSITY: |
560 | if (data->als_contr & LTR501_CONTR_ALS_GAIN_MASK) { | 610 | i = (data->als_contr & data->chip_info->als_gain_mask) |
561 | *val = 0; | 611 | >> data->chip_info->als_gain_shift; |
562 | *val2 = 5000; | 612 | *val = data->chip_info->als_gain[i].scale; |
563 | return IIO_VAL_INT_PLUS_MICRO; | 613 | *val2 = data->chip_info->als_gain[i].uscale; |
564 | } | 614 | return IIO_VAL_INT_PLUS_MICRO; |
565 | *val = 1; | ||
566 | *val2 = 0; | ||
567 | return IIO_VAL_INT; | ||
568 | case IIO_PROXIMITY: | 615 | case IIO_PROXIMITY: |
569 | i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >> | 616 | i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >> |
570 | LTR501_CONTR_PS_GAIN_SHIFT; | 617 | LTR501_CONTR_PS_GAIN_SHIFT; |
571 | *val = ltr501_ps_gain[i][0]; | 618 | *val = data->chip_info->ps_gain[i].scale; |
572 | *val2 = ltr501_ps_gain[i][1]; | 619 | *val2 = data->chip_info->ps_gain[i].uscale; |
573 | return IIO_VAL_INT_PLUS_MICRO; | 620 | return IIO_VAL_INT_PLUS_MICRO; |
574 | default: | 621 | default: |
575 | return -EINVAL; | 622 | return -EINVAL; |
@@ -594,12 +641,13 @@ static int ltr501_read_raw(struct iio_dev *indio_dev, | |||
594 | return -EINVAL; | 641 | return -EINVAL; |
595 | } | 642 | } |
596 | 643 | ||
597 | static int ltr501_get_ps_gain_index(int val, int val2) | 644 | static int ltr501_get_gain_index(struct ltr501_gain *gain, int size, |
645 | int val, int val2) | ||
598 | { | 646 | { |
599 | int i; | 647 | int i; |
600 | 648 | ||
601 | for (i = 0; i < ARRAY_SIZE(ltr501_ps_gain); i++) | 649 | for (i = 0; i < size; i++) |
602 | if (val == ltr501_ps_gain[i][0] && val2 == ltr501_ps_gain[i][1]) | 650 | if (val == gain[i].scale && val2 == gain[i].uscale) |
603 | return i; | 651 | return i; |
604 | 652 | ||
605 | return -1; | 653 | return -1; |
@@ -611,6 +659,7 @@ static int ltr501_write_raw(struct iio_dev *indio_dev, | |||
611 | { | 659 | { |
612 | struct ltr501_data *data = iio_priv(indio_dev); | 660 | struct ltr501_data *data = iio_priv(indio_dev); |
613 | int i, ret, freq_val, freq_val2; | 661 | int i, ret, freq_val, freq_val2; |
662 | struct ltr501_chip_info *info = data->chip_info; | ||
614 | 663 | ||
615 | if (iio_buffer_enabled(indio_dev)) | 664 | if (iio_buffer_enabled(indio_dev)) |
616 | return -EBUSY; | 665 | return -EBUSY; |
@@ -619,17 +668,21 @@ static int ltr501_write_raw(struct iio_dev *indio_dev, | |||
619 | case IIO_CHAN_INFO_SCALE: | 668 | case IIO_CHAN_INFO_SCALE: |
620 | switch (chan->type) { | 669 | switch (chan->type) { |
621 | case IIO_INTENSITY: | 670 | case IIO_INTENSITY: |
622 | if (val == 0 && val2 == 5000) | 671 | i = ltr501_get_gain_index(info->als_gain, |
623 | data->als_contr |= LTR501_CONTR_ALS_GAIN_MASK; | 672 | info->als_gain_tbl_size, |
624 | else if (val == 1 && val2 == 0) | 673 | val, val2); |
625 | data->als_contr &= ~LTR501_CONTR_ALS_GAIN_MASK; | 674 | if (i < 0) |
626 | else | ||
627 | return -EINVAL; | 675 | return -EINVAL; |
628 | 676 | ||
677 | data->als_contr &= ~info->als_gain_mask; | ||
678 | data->als_contr |= i << info->als_gain_shift; | ||
679 | |||
629 | return regmap_write(data->regmap, LTR501_ALS_CONTR, | 680 | return regmap_write(data->regmap, LTR501_ALS_CONTR, |
630 | data->als_contr); | 681 | data->als_contr); |
631 | case IIO_PROXIMITY: | 682 | case IIO_PROXIMITY: |
632 | i = ltr501_get_ps_gain_index(val, val2); | 683 | i = ltr501_get_gain_index(info->ps_gain, |
684 | info->ps_gain_tbl_size, | ||
685 | val, val2); | ||
633 | if (i < 0) | 686 | if (i < 0) |
634 | return -EINVAL; | 687 | return -EINVAL; |
635 | data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK; | 688 | data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK; |
@@ -927,14 +980,61 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev, | |||
927 | return -EINVAL; | 980 | return -EINVAL; |
928 | } | 981 | } |
929 | 982 | ||
930 | static IIO_CONST_ATTR(in_proximity_scale_available, "1 0.25 0.125 0.0625"); | 983 | static ssize_t ltr501_show_proximity_scale_avail(struct device *dev, |
931 | static IIO_CONST_ATTR(in_intensity_scale_available, "1 0.005"); | 984 | struct device_attribute *attr, |
985 | char *buf) | ||
986 | { | ||
987 | struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); | ||
988 | struct ltr501_chip_info *info = data->chip_info; | ||
989 | ssize_t len = 0; | ||
990 | int i; | ||
991 | |||
992 | for (i = 0; i < info->ps_gain_tbl_size; i++) { | ||
993 | if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN) | ||
994 | continue; | ||
995 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", | ||
996 | info->ps_gain[i].scale, | ||
997 | info->ps_gain[i].uscale); | ||
998 | } | ||
999 | |||
1000 | buf[len - 1] = '\n'; | ||
1001 | |||
1002 | return len; | ||
1003 | } | ||
1004 | |||
1005 | static ssize_t ltr501_show_intensity_scale_avail(struct device *dev, | ||
1006 | struct device_attribute *attr, | ||
1007 | char *buf) | ||
1008 | { | ||
1009 | struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); | ||
1010 | struct ltr501_chip_info *info = data->chip_info; | ||
1011 | ssize_t len = 0; | ||
1012 | int i; | ||
1013 | |||
1014 | for (i = 0; i < info->als_gain_tbl_size; i++) { | ||
1015 | if (info->als_gain[i].scale == LTR501_RESERVED_GAIN) | ||
1016 | continue; | ||
1017 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", | ||
1018 | info->als_gain[i].scale, | ||
1019 | info->als_gain[i].uscale); | ||
1020 | } | ||
1021 | |||
1022 | buf[len - 1] = '\n'; | ||
1023 | |||
1024 | return len; | ||
1025 | } | ||
1026 | |||
932 | static IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4"); | 1027 | static IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4"); |
933 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5"); | 1028 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5"); |
934 | 1029 | ||
1030 | static IIO_DEVICE_ATTR(in_proximity_scale_available, S_IRUGO, | ||
1031 | ltr501_show_proximity_scale_avail, NULL, 0); | ||
1032 | static IIO_DEVICE_ATTR(in_intensity_scale_available, S_IRUGO, | ||
1033 | ltr501_show_intensity_scale_avail, NULL, 0); | ||
1034 | |||
935 | static struct attribute *ltr501_attributes[] = { | 1035 | static struct attribute *ltr501_attributes[] = { |
936 | &iio_const_attr_in_proximity_scale_available.dev_attr.attr, | 1036 | &iio_dev_attr_in_proximity_scale_available.dev_attr.attr, |
937 | &iio_const_attr_in_intensity_scale_available.dev_attr.attr, | 1037 | &iio_dev_attr_in_intensity_scale_available.dev_attr.attr, |
938 | &iio_const_attr_integration_time_available.dev_attr.attr, | 1038 | &iio_const_attr_integration_time_available.dev_attr.attr, |
939 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | 1039 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
940 | NULL | 1040 | NULL |
@@ -962,6 +1062,29 @@ static const struct iio_info ltr501_info = { | |||
962 | .driver_module = THIS_MODULE, | 1062 | .driver_module = THIS_MODULE, |
963 | }; | 1063 | }; |
964 | 1064 | ||
1065 | static struct ltr501_chip_info ltr501_chip_info_tbl[] = { | ||
1066 | [ltr501] = { | ||
1067 | .partid = 0x08, | ||
1068 | .als_gain = ltr501_als_gain_tbl, | ||
1069 | .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl), | ||
1070 | .ps_gain = ltr501_ps_gain_tbl, | ||
1071 | .ps_gain_tbl_size = ARRAY_SIZE(ltr501_ps_gain_tbl), | ||
1072 | .als_mode_active = BIT(0) | BIT(1), | ||
1073 | .als_gain_mask = BIT(3), | ||
1074 | .als_gain_shift = 3, | ||
1075 | }, | ||
1076 | [ltr559] = { | ||
1077 | .partid = 0x09, | ||
1078 | .als_gain = ltr559_als_gain_tbl, | ||
1079 | .als_gain_tbl_size = ARRAY_SIZE(ltr559_als_gain_tbl), | ||
1080 | .ps_gain = ltr559_ps_gain_tbl, | ||
1081 | .ps_gain_tbl_size = ARRAY_SIZE(ltr559_ps_gain_tbl), | ||
1082 | .als_mode_active = BIT(1), | ||
1083 | .als_gain_mask = BIT(2) | BIT(3) | BIT(4), | ||
1084 | .als_gain_shift = 2, | ||
1085 | }, | ||
1086 | }; | ||
1087 | |||
965 | static int ltr501_write_contr(struct ltr501_data *data, u8 als_val, u8 ps_val) | 1088 | static int ltr501_write_contr(struct ltr501_data *data, u8 als_val, u8 ps_val) |
966 | { | 1089 | { |
967 | int ret; | 1090 | int ret; |
@@ -1062,7 +1185,7 @@ static int ltr501_init(struct ltr501_data *data) | |||
1062 | if (ret < 0) | 1185 | if (ret < 0) |
1063 | return ret; | 1186 | return ret; |
1064 | 1187 | ||
1065 | data->als_contr = status | LTR501_CONTR_ACTIVE; | 1188 | data->als_contr = ret | data->chip_info->als_mode_active; |
1066 | 1189 | ||
1067 | ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status); | 1190 | ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status); |
1068 | if (ret < 0) | 1191 | if (ret < 0) |
@@ -1105,17 +1228,30 @@ static struct regmap_config ltr501_regmap_config = { | |||
1105 | 1228 | ||
1106 | static int ltr501_powerdown(struct ltr501_data *data) | 1229 | static int ltr501_powerdown(struct ltr501_data *data) |
1107 | { | 1230 | { |
1108 | return ltr501_write_contr(data, data->als_contr & ~LTR501_CONTR_ACTIVE, | 1231 | return ltr501_write_contr(data, data->als_contr & |
1232 | ~data->chip_info->als_mode_active, | ||
1109 | data->ps_contr & ~LTR501_CONTR_ACTIVE); | 1233 | data->ps_contr & ~LTR501_CONTR_ACTIVE); |
1110 | } | 1234 | } |
1111 | 1235 | ||
1236 | static const char *ltr501_match_acpi_device(struct device *dev, int *chip_idx) | ||
1237 | { | ||
1238 | const struct acpi_device_id *id; | ||
1239 | |||
1240 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | ||
1241 | if (!id) | ||
1242 | return NULL; | ||
1243 | *chip_idx = id->driver_data; | ||
1244 | return dev_name(dev); | ||
1245 | } | ||
1246 | |||
1112 | static int ltr501_probe(struct i2c_client *client, | 1247 | static int ltr501_probe(struct i2c_client *client, |
1113 | const struct i2c_device_id *id) | 1248 | const struct i2c_device_id *id) |
1114 | { | 1249 | { |
1115 | struct ltr501_data *data; | 1250 | struct ltr501_data *data; |
1116 | struct iio_dev *indio_dev; | 1251 | struct iio_dev *indio_dev; |
1117 | struct regmap *regmap; | 1252 | struct regmap *regmap; |
1118 | int ret, partid; | 1253 | int ret, partid, chip_idx = 0; |
1254 | const char *name = NULL; | ||
1119 | 1255 | ||
1120 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | 1256 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
1121 | if (!indio_dev) | 1257 | if (!indio_dev) |
@@ -1186,13 +1322,25 @@ static int ltr501_probe(struct i2c_client *client, | |||
1186 | ret = regmap_read(data->regmap, LTR501_PART_ID, &partid); | 1322 | ret = regmap_read(data->regmap, LTR501_PART_ID, &partid); |
1187 | if (ret < 0) | 1323 | if (ret < 0) |
1188 | return ret; | 1324 | return ret; |
1189 | if ((partid >> 4) != 0x8) | 1325 | |
1326 | if (id) { | ||
1327 | name = id->name; | ||
1328 | chip_idx = id->driver_data; | ||
1329 | } else if (ACPI_HANDLE(&client->dev)) { | ||
1330 | name = ltr501_match_acpi_device(&client->dev, &chip_idx); | ||
1331 | } else { | ||
1332 | return -ENODEV; | ||
1333 | } | ||
1334 | |||
1335 | data->chip_info = <r501_chip_info_tbl[chip_idx]; | ||
1336 | |||
1337 | if ((partid >> 4) != data->chip_info->partid) | ||
1190 | return -ENODEV; | 1338 | return -ENODEV; |
1191 | 1339 | ||
1192 | indio_dev->dev.parent = &client->dev; | 1340 | indio_dev->dev.parent = &client->dev; |
1193 | indio_dev->channels = ltr501_channels; | 1341 | indio_dev->channels = ltr501_channels; |
1194 | indio_dev->num_channels = ARRAY_SIZE(ltr501_channels); | 1342 | indio_dev->num_channels = ARRAY_SIZE(ltr501_channels); |
1195 | indio_dev->name = LTR501_DRV_NAME; | 1343 | indio_dev->name = name; |
1196 | indio_dev->modes = INDIO_DIRECT_MODE; | 1344 | indio_dev->modes = INDIO_DIRECT_MODE; |
1197 | 1345 | ||
1198 | ret = ltr501_init(data); | 1346 | ret = ltr501_init(data); |
@@ -1266,13 +1414,15 @@ static int ltr501_resume(struct device *dev) | |||
1266 | static SIMPLE_DEV_PM_OPS(ltr501_pm_ops, ltr501_suspend, ltr501_resume); | 1414 | static SIMPLE_DEV_PM_OPS(ltr501_pm_ops, ltr501_suspend, ltr501_resume); |
1267 | 1415 | ||
1268 | static const struct acpi_device_id ltr_acpi_match[] = { | 1416 | static const struct acpi_device_id ltr_acpi_match[] = { |
1269 | {"LTER0501", 0}, | 1417 | {"LTER0501", ltr501}, |
1418 | {"LTER0559", ltr559}, | ||
1270 | { }, | 1419 | { }, |
1271 | }; | 1420 | }; |
1272 | MODULE_DEVICE_TABLE(acpi, ltr_acpi_match); | 1421 | MODULE_DEVICE_TABLE(acpi, ltr_acpi_match); |
1273 | 1422 | ||
1274 | static const struct i2c_device_id ltr501_id[] = { | 1423 | static const struct i2c_device_id ltr501_id[] = { |
1275 | { "ltr501", 0 }, | 1424 | { "ltr501", ltr501}, |
1425 | { "ltr559", ltr559}, | ||
1276 | { } | 1426 | { } |
1277 | }; | 1427 | }; |
1278 | MODULE_DEVICE_TABLE(i2c, ltr501_id); | 1428 | MODULE_DEVICE_TABLE(i2c, ltr501_id); |