aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorVivien Didelot <vivien.didelot@savoirfairelinux.com>2013-10-20 14:02:00 -0400
committerJonathan Cameron <jic23@kernel.org>2013-10-20 17:36:21 -0400
commit61bdda69222c09efd8943d240cd2a8e0bb659d82 (patch)
tree9441f5e30d362e800f0aa88681ceef0050421fca /drivers/iio
parenteb03610a9ceb4ebbe70c4a68a5bf9fb509a233c3 (diff)
iio:adc:max1363 support SMBus for 8-bit devices
The driver currently supports only I2C access. But supported devices with an accuracy of 8-bit are compatible with the SMBus byte access routines. This patch wraps the send and receive routines depending on the chip accuracy and fonctionnalities of its adapter. For instance, this allows us to use a MAX11603 on a ICH7 controller. This patch also simplifies the max1363_write_basic_config() routine to use the struct max1363_state fields directly. Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/adc/max1363.c76
1 files changed, 57 insertions, 19 deletions
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index cc07b3765fe0..6118dced02b6 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -165,6 +165,8 @@ struct max1363_chip_info {
165 * @thresh_low: low threshold values 165 * @thresh_low: low threshold values
166 * @vref: Reference voltage regulator 166 * @vref: Reference voltage regulator
167 * @vref_uv: Actual (external or internal) reference voltage 167 * @vref_uv: Actual (external or internal) reference voltage
168 * @send: function used to send data to the chip
169 * @recv: function used to receive data from the chip
168 */ 170 */
169struct max1363_state { 171struct max1363_state {
170 struct i2c_client *client; 172 struct i2c_client *client;
@@ -186,6 +188,10 @@ struct max1363_state {
186 s16 thresh_low[8]; 188 s16 thresh_low[8];
187 struct regulator *vref; 189 struct regulator *vref;
188 u32 vref_uv; 190 u32 vref_uv;
191 int (*send)(const struct i2c_client *client,
192 const char *buf, int count);
193 int (*recv)(const struct i2c_client *client,
194 char *buf, int count);
189}; 195};
190 196
191#define MAX1363_MODE_SINGLE(_num, _mask) { \ 197#define MAX1363_MODE_SINGLE(_num, _mask) { \
@@ -311,13 +317,37 @@ static const struct max1363_mode
311 return NULL; 317 return NULL;
312} 318}
313 319
314static int max1363_write_basic_config(struct i2c_client *client, 320static int max1363_smbus_send(const struct i2c_client *client, const char *buf,
315 unsigned char d1, 321 int count)
316 unsigned char d2)
317{ 322{
318 u8 tx_buf[2] = {d1, d2}; 323 int i, err;
319 324
320 return i2c_master_send(client, tx_buf, 2); 325 for (i = err = 0; err == 0 && i < count; ++i)
326 err = i2c_smbus_write_byte(client, buf[i]);
327
328 return err ? err : count;
329}
330
331static int max1363_smbus_recv(const struct i2c_client *client, char *buf,
332 int count)
333{
334 int i, ret;
335
336 for (i = 0; i < count; ++i) {
337 ret = i2c_smbus_read_byte(client);
338 if (ret < 0)
339 return ret;
340 buf[i] = ret;
341 }
342
343 return count;
344}
345
346static int max1363_write_basic_config(struct max1363_state *st)
347{
348 u8 tx_buf[2] = { st->setupbyte, st->configbyte };
349
350 return st->send(st->client, tx_buf, 2);
321} 351}
322 352
323static int max1363_set_scan_mode(struct max1363_state *st) 353static int max1363_set_scan_mode(struct max1363_state *st)
@@ -327,9 +357,7 @@ static int max1363_set_scan_mode(struct max1363_state *st)
327 | MAX1363_SE_DE_MASK); 357 | MAX1363_SE_DE_MASK);
328 st->configbyte |= st->current_mode->conf; 358 st->configbyte |= st->current_mode->conf;
329 359
330 return max1363_write_basic_config(st->client, 360 return max1363_write_basic_config(st);
331 st->setupbyte,
332 st->configbyte);
333} 361}
334 362
335static int max1363_read_single_chan(struct iio_dev *indio_dev, 363static int max1363_read_single_chan(struct iio_dev *indio_dev,
@@ -366,7 +394,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
366 } 394 }
367 if (st->chip_info->bits != 8) { 395 if (st->chip_info->bits != 8) {
368 /* Get reading */ 396 /* Get reading */
369 data = i2c_master_recv(client, rxbuf, 2); 397 data = st->recv(client, rxbuf, 2);
370 if (data < 0) { 398 if (data < 0) {
371 ret = data; 399 ret = data;
372 goto error_ret; 400 goto error_ret;
@@ -375,7 +403,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
375 ((1 << st->chip_info->bits) - 1); 403 ((1 << st->chip_info->bits) - 1);
376 } else { 404 } else {
377 /* Get reading */ 405 /* Get reading */
378 data = i2c_master_recv(client, rxbuf, 1); 406 data = st->recv(client, rxbuf, 1);
379 if (data < 0) { 407 if (data < 0) {
380 ret = data; 408 ret = data;
381 goto error_ret; 409 goto error_ret;
@@ -772,11 +800,11 @@ static irqreturn_t max1363_event_handler(int irq, void *private)
772 u8 tx[2] = { st->setupbyte, 800 u8 tx[2] = { st->setupbyte,
773 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 }; 801 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
774 802
775 i2c_master_recv(st->client, &rx, 1); 803 st->recv(st->client, &rx, 1);
776 mask = rx; 804 mask = rx;
777 for_each_set_bit(loc, &mask, 8) 805 for_each_set_bit(loc, &mask, 8)
778 iio_push_event(indio_dev, max1363_event_codes[loc], timestamp); 806 iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
779 i2c_master_send(st->client, tx, 2); 807 st->send(st->client, tx, 2);
780 808
781 return IRQ_HANDLED; 809 return IRQ_HANDLED;
782} 810}
@@ -812,9 +840,7 @@ static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
812 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP; 840 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
813 st->configbyte &= ~MAX1363_SCAN_MASK; 841 st->configbyte &= ~MAX1363_SCAN_MASK;
814 st->monitor_on = false; 842 st->monitor_on = false;
815 return max1363_write_basic_config(st->client, 843 return max1363_write_basic_config(st);
816 st->setupbyte,
817 st->configbyte);
818 } 844 }
819 845
820 /* Ensure we are in the relevant mode */ 846 /* Ensure we are in the relevant mode */
@@ -876,7 +902,7 @@ static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
876 } 902 }
877 903
878 904
879 ret = i2c_master_send(st->client, tx_buf, len); 905 ret = st->send(st->client, tx_buf, len);
880 if (ret < 0) 906 if (ret < 0)
881 goto error_ret; 907 goto error_ret;
882 if (ret != len) { 908 if (ret != len) {
@@ -893,7 +919,7 @@ static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
893 */ 919 */
894 tx_buf[0] = st->setupbyte; 920 tx_buf[0] = st->setupbyte;
895 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0; 921 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
896 ret = i2c_master_send(st->client, tx_buf, 2); 922 ret = st->send(st->client, tx_buf, 2);
897 if (ret < 0) 923 if (ret < 0)
898 goto error_ret; 924 goto error_ret;
899 if (ret != 2) { 925 if (ret != 2) {
@@ -1481,9 +1507,9 @@ static irqreturn_t max1363_trigger_handler(int irq, void *p)
1481 if (rxbuf == NULL) 1507 if (rxbuf == NULL)
1482 goto done; 1508 goto done;
1483 if (st->chip_info->bits != 8) 1509 if (st->chip_info->bits != 8)
1484 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2); 1510 b_sent = st->recv(st->client, rxbuf, numvals * 2);
1485 else 1511 else
1486 b_sent = i2c_master_recv(st->client, rxbuf, numvals); 1512 b_sent = st->recv(st->client, rxbuf, numvals);
1487 if (b_sent < 0) 1513 if (b_sent < 0)
1488 goto done_free; 1514 goto done_free;
1489 1515
@@ -1550,6 +1576,18 @@ static int max1363_probe(struct i2c_client *client,
1550 st->vref_uv = vref_uv; 1576 st->vref_uv = vref_uv;
1551 } 1577 }
1552 1578
1579 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1580 st->send = i2c_master_send;
1581 st->recv = i2c_master_recv;
1582 } else if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)
1583 && st->chip_info->bits == 8) {
1584 st->send = max1363_smbus_send;
1585 st->recv = max1363_smbus_recv;
1586 } else {
1587 ret = -EOPNOTSUPP;
1588 goto error_disable_reg;
1589 }
1590
1553 ret = max1363_alloc_scan_masks(indio_dev); 1591 ret = max1363_alloc_scan_masks(indio_dev);
1554 if (ret) 1592 if (ret)
1555 goto error_disable_reg; 1593 goto error_disable_reg;