Show a patch.

GET /patchwork/api/1.1/patches/1035986/
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1035986,
    "url": "https://lore.kernel.org/patchwork/api/1.1/patches/1035986/",
    "web_url": "https://lore.kernel.org/patchwork/patch/1035986/",
    "project": {
        "id": 1,
        "url": "https://lore.kernel.org/patchwork/api/1.1/projects/1/",
        "name": "Linux Kernel Mailing List",
        "link_name": "lkml",
        "list_id": "linux-kernel.vger.kernel.org",
        "list_email": "linux-kernel@vger.kernel.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<2b847f2b4ed5a94d21c41274395b97eed3ec39dd.1548580142.git.bobbyeshleman@gmail.com>",
    "date": "2019-01-27T17:06:56",
    "name": "[v3,1/2] Add driver support for MAX44009 light sensor",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "e757861a2360b597df367ff265bd1fd9ef44c1f3",
    "submitter": {
        "id": 24999,
        "url": "https://lore.kernel.org/patchwork/api/1.1/people/24999/",
        "name": "Robert Eshleman",
        "email": "bobbyeshleman@gmail.com"
    },
    "delegate": null,
    "mbox": "https://lore.kernel.org/patchwork/patch/1035986/mbox/",
    "series": [
        {
            "id": 381121,
            "url": "https://lore.kernel.org/patchwork/api/1.1/series/381121/",
            "date": "2019-01-27T17:06:56",
            "name": "[v3,1/2] Add driver support for MAX44009 light sensor",
            "version": 3,
            "mbox": "https://lore.kernel.org/patchwork/series/381121/mbox/"
        }
    ],
    "comments": "https://lore.kernel.org/patchwork/api/patches/1035986/comments/",
    "check": "pending",
    "checks": "https://lore.kernel.org/patchwork/api/patches/1035986/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<SRS0=AJhT=QD=vger.kernel.org=linux-kernel-owner@kernel.org>",
        "Received": [
            "from mail.kernel.org (mail.kernel.org [198.145.29.99])\n\tby smtp.lore.kernel.org (Postfix) with ESMTP id 51335C282CC\n\tfor <linux-kernel@archiver.kernel.org>; Sun, 27 Jan 2019 17:07:02 +0000 (UTC)",
            "from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby mail.kernel.org (Postfix) with ESMTP id 03BE4214C6\n\tfor <linux-kernel@archiver.kernel.org>; Sun, 27 Jan 2019 17:07:02 +0000 (UTC)",
            "(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n        id S1726946AbfA0RHA (ORCPT\n        <rfc822;linux-kernel@archiver.kernel.org>);\n        Sun, 27 Jan 2019 12:07:00 -0500",
            "from mail-pl1-f194.google.com ([209.85.214.194]:44209 \"EHLO\n        mail-pl1-f194.google.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n        with ESMTP id S1726357AbfA0RHA (ORCPT\n        <rfc822;linux-kernel@vger.kernel.org>);\n        Sun, 27 Jan 2019 12:07:00 -0500",
            "by mail-pl1-f194.google.com with SMTP id e11so6679782plt.11;\n        Sun, 27 Jan 2019 09:06:59 -0800 (PST)",
            "from bobby.localdomain ([2601:1c0:5501:37e2::745a])\n        by smtp.gmail.com with ESMTPSA id\n a17sm35811701pgm.26.2019.01.27.09.06.57\n        (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);\n        Sun, 27 Jan 2019 09:06:58 -0800 (PST)"
        ],
        "Authentication-Results": "mail.kernel.org;\n\tdkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com\n header.b=\"VJGlWRem\"",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n        d=gmail.com; s=20161025;\n        h=date:from:to:cc:subject:message-id:mime-version:content-disposition\n         :user-agent;\n        bh=20x3jbVZsSLGt8E1QkaKK/CLwb5zYNsvI249s2RQkTE=;\n        b=VJGlWRemrCc2IOGUymllxl4/oAI7bpLOh3HRdzsuZSA80ihUshYDise3FLK2pnTo1r\n         NFNKIGO6k0bInrQ8udtnItovMJNZpQ9NG5tqoCgKvqbjFLCc4+DiTFGe34GZY47dNMC3\n         8tksD5fxnmBXZNjXmHYkOQAm0qP5mw+mdLX1KYp5G67+pjvsT9BA6BYYFaNailJ2HgFO\n         Qh69aMwx/+wh+Jv5IzNbzYBj4KY9h75urmrL+jk+EpkRD16TrdVv2f3iV1H5BZcvMXOj\n         Gs0dtNJmQxjDYr/USgRbbYHur6s7+DPs8OBNvXe3tOe6mL4vYTws/tlAt0CSglyENjuB\n         d1yg==",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n        d=1e100.net; s=20161025;\n        h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version\n         :content-disposition:user-agent;\n        bh=20x3jbVZsSLGt8E1QkaKK/CLwb5zYNsvI249s2RQkTE=;\n        b=r9IhkUgNIs0NLTq0emQPc9eyKB5cJkyV3ngvy8mzQqY78hOH2dOjP99y8fja2zABSr\n         DQiwA5PkKhXo5/PIeTCMQv4x99s7I4gS2icp/xJh+SGDLr/i5/By+4R+5yzFCaAd4mSJ\n         DDcwzVTNRGdjlpgT5VUUp9mrB+IOSe6rV1KO8OGqF5jXHBe/6MTmyYnHIG7YKzB6Po9l\n         RPcK0Kt3EgfLPXXHpuj6/Sfao36Q64gGzsMwVecqd/PzDKA1uUWJcrtKeqzbrQRnQ0l3\n         BtuN33ZogDkdos0MxWyMQgeNspRvsQ/kI4DQKmdI789h36kzGvVVgvP9vRXyK+EcEdRP\n         wlIg==",
        "X-Gm-Message-State": "AJcUukcnpp/BbbGj5c5zBBrOkGoTbgzEJGa7mK2qnr66L1gaxUdjD7Xw\n        UMYIUSj/AJVmxBsiCMErKk3yJuhfi3A=",
        "X-Google-Smtp-Source": "\n ALg8bN4wjDn/lalEm71W9kMW8C4A3jFEsHkUoI4H40is9WBMHpta1kW2kpj9apTFStNsURSEWpjTNg==",
        "X-Received": "by 2002:a17:902:33c1:: with SMTP id\n b59mr18513580plc.220.1548608818782;\n        Sun, 27 Jan 2019 09:06:58 -0800 (PST)",
        "Date": "Sun, 27 Jan 2019 09:06:56 -0800",
        "From": "Robert Eshleman <bobbyeshleman@gmail.com>",
        "To": "bobbyeshleman@gmail.com",
        "Cc": "Jonathan Cameron <jic23@kernel.org>,\n        Hartmut Knaack <knaack.h@gmx.de>,\n        Lars-Peter Clausen <lars@metafoo.de>,\n        Peter Meerwald-Stadler <pmeerw@pmeerw.net>,\n        linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org",
        "Subject": "[PATCH v3 1/2] Add driver support for MAX44009 light sensor",
        "Message-ID": "\n <2b847f2b4ed5a94d21c41274395b97eed3ec39dd.1548580142.git.bobbyeshleman@gmail.com>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=us-ascii",
        "Content-Disposition": "inline",
        "User-Agent": "Mutt/1.11.2 (2019-01-07)",
        "Sender": "linux-kernel-owner@vger.kernel.org",
        "Precedence": "bulk",
        "List-ID": "<linux-kernel.vger.kernel.org>",
        "X-Mailing-List": "linux-kernel@vger.kernel.org"
    },
    "content": "The MAX44009 is a low-power ambient light sensor from Maxim\nIntegrated. It differs from the MAX44000 in that it doesn't have\nproximity sensing and that it requires far less current (1 micro-amp\nvs 5 micro-amps). The register mapping and feature set between the\ntwo are different enough to require a new driver for the MAX44009.\n\nDeveloped and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)\n\nSupported features:\n\n* Reading lux (processed value)\n\n* Rising and falling illuminance threshold\n  events\n\n* Configuring integration time\n\nhttps://datasheets.maximintegrated.com/en/ds/MAX44009.pdf\n\nSigned-off-by: Robert Eshleman <bobbyeshleman@gmail.com>\n---\nChanges to v3:\n- Remove unnecessary mask when setting integration time\n- Return IRQ_NONE in IRQ handler when interrupt not from this device\n- Fix incorrect register usage when reading lux low byte\n- Clean up style\n\nChanges to v2:\n- Remove unnecessary mutex locking\n- Remove unnecessary triggered buffer support\n- Remove unnecessary wrapper functions for smbus reads and writes\n- Remove scale avail support\n- Use constant scale to provide processed values instead of raw\n- Remove IRQ_NONE return for non-shared IRQ\n- Clean up style\n\n drivers/iio/light/Kconfig    |  10 +\n drivers/iio/light/Makefile   |   1 +\n drivers/iio/light/max44009.c | 555 +++++++++++++++++++++++++++++++++++\n 3 files changed, 566 insertions(+)\n create mode 100644 drivers/iio/light/max44009.c",
    "diff": "diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig\nindex 36f458433480..5190eacfeb0a 100644\n--- a/drivers/iio/light/Kconfig\n+++ b/drivers/iio/light/Kconfig\n@@ -299,6 +299,16 @@ config MAX44000\n \t To compile this driver as a module, choose M here:\n \t the module will be called max44000.\n \n+config MAX44009\n+\ttristate \"MAX44009 Ambient Light Sensor\"\n+\tdepends on I2C\n+\thelp\n+\t Say Y here if you want to build support for Maxim Integrated's\n+\t MAX44009 ambient light sensor device.\n+\n+\t To compile this driver as a module, choose M here:\n+\t the module will be called max44009.\n+\n config OPT3001\n \ttristate \"Texas Instruments OPT3001 Light Sensor\"\n \tdepends on I2C\ndiff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile\nindex 286bf3975372..e40794fbb435 100644\n--- a/drivers/iio/light/Makefile\n+++ b/drivers/iio/light/Makefile\n@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533)\t+= lm3533-als.o\n obj-$(CONFIG_LTR501)\t\t+= ltr501.o\n obj-$(CONFIG_LV0104CS)\t\t+= lv0104cs.o\n obj-$(CONFIG_MAX44000)\t\t+= max44000.o\n+obj-$(CONFIG_MAX44009)\t\t+= max44009.o\n obj-$(CONFIG_OPT3001)\t\t+= opt3001.o\n obj-$(CONFIG_PA12203001)\t+= pa12203001.o\n obj-$(CONFIG_RPR0521)\t\t+= rpr0521.o\ndiff --git a/drivers/iio/light/max44009.c b/drivers/iio/light/max44009.c\nnew file mode 100644\nindex 000000000000..fdbe2b840b02\n--- /dev/null\n+++ b/drivers/iio/light/max44009.c\n@@ -0,0 +1,555 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * max44009.c - Support for MAX44009 Ambient Light Sensor\n+ *\n+ * Copyright (c) 2019 Robert Eshleman <bobbyeshleman@gmail.com>\n+ *\n+ * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf\n+ *\n+ * TODO: Support continuous mode and configuring from manual mode to\n+ * \t automatic mode.\n+ *\n+ * Default I2C address: 0x4a\n+ */\n+\n+#include <linux/init.h>\n+#include <linux/kernel.h>\n+#include <linux/bits.h>\n+#include <linux/i2c.h>\n+#include <linux/iio/events.h>\n+#include <linux/iio/iio.h>\n+#include <linux/iio/sysfs.h>\n+#include <linux/interrupt.h>\n+#include <linux/module.h>\n+#include <linux/util_macros.h>\n+\n+#define MAX44009_DRV_NAME \"max44009\"\n+\n+/* Registers in datasheet order */\n+#define MAX44009_REG_INT_STATUS 0x0\n+#define MAX44009_REG_INT_EN 0x1\n+#define MAX44009_REG_CFG 0x2\n+#define MAX44009_REG_LUX_HI 0x3\n+#define MAX44009_REG_LUX_LO 0x4\n+#define MAX44009_REG_UPPER_THR 0x5\n+#define MAX44009_REG_LOWER_THR 0x6\n+#define MAX44009_REG_THR_TIMER 0x7\n+\n+#define MAX44009_CFG_TIM_MASK GENMASK(2, 0)\n+#define MAX44009_CFG_MAN_MODE_MASK BIT(6)\n+\n+/* The maximum rising threshold for the max44009 */\n+#define MAX44009_MAXIMUM_THRESHOLD 7520256\n+\n+#define MAX44009_THRESH_EXP_MASK (0xf << 4)\n+#define MAX44009_THRESH_EXP_RSHIFT 4\n+#define MAX44009_THRESH_MANT_LSHIFT 4\n+#define MAX44009_THRESH_MANT_MASK 0xf\n+\n+#define MAX44009_UPPER_THR_MINIMUM 15\n+\n+/* The max44009 always scales raw readings by 0.045 and is non-configurable */\n+#define MAX44009_SCALE_NUMERATOR 45\n+#define MAX44009_SCALE_DENOMINATOR 1000\n+\n+/* The fixed-point fractional multiplier for de-scaling threshold values */\n+#define MAX44009_FRACT_MULT 1000000\n+\n+static const u32 max44009_int_time_ns_array[] = {\n+\t800000000,\n+\t400000000,\n+\t200000000,\n+\t100000000,\n+\t50000000, /* Manual mode only */\n+\t25000000, /* Manual mode only */\n+\t12500000, /* Manual mode only */\n+\t6250000,  /* Manual mode only */\n+};\n+\n+static const char max44009_int_time_str[] =\n+\t\"0.8 \"\n+\t\"0.4 \"\n+\t\"0.2 \"\n+\t\"0.1 \"\n+\t\"0.05 \"\n+\t\"0.025 \"\n+\t\"0.0125 \"\n+\t\"0.00625\";\n+\n+struct max44009_data {\n+\tstruct i2c_client *client;\n+\tstruct mutex lock;\n+};\n+\n+static const struct iio_event_spec max44009_event_spec[] = {\n+\t{\n+\t\t.type = IIO_EV_TYPE_THRESH,\n+\t\t.dir = IIO_EV_DIR_RISING,\n+\t\t.mask_separate = BIT(IIO_EV_INFO_VALUE) |\n+\t\t\t\t BIT(IIO_EV_INFO_ENABLE),\n+\t},\n+\t{\n+\t\t.type = IIO_EV_TYPE_THRESH,\n+\t\t.dir = IIO_EV_DIR_FALLING,\n+\t\t.mask_separate = BIT(IIO_EV_INFO_VALUE) |\n+\t\t\t\t BIT(IIO_EV_INFO_ENABLE),\n+\t},\n+};\n+\n+static const struct iio_chan_spec max44009_channels[] = {\n+\t{\n+\t\t.type = IIO_LIGHT,\n+\t\t.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |\n+\t\t\t\t      BIT(IIO_CHAN_INFO_INT_TIME),\n+\t\t.event_spec = max44009_event_spec,\n+\t\t.num_event_specs = ARRAY_SIZE(max44009_event_spec),\n+\t},\n+};\n+\n+static int max44009_read_int_time(struct max44009_data *data)\n+{\n+\n+\tint ret = i2c_smbus_read_byte_data(data->client, MAX44009_REG_CFG);\n+\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\treturn max44009_int_time_ns_array[ret & MAX44009_CFG_TIM_MASK];\n+}\n+\n+static int max44009_write_int_time(struct max44009_data *data,\n+\t\t\t\t   int val, int val2)\n+{\n+\tstruct i2c_client *client = data->client;\n+\tint ret, int_time, config;\n+\ts64 ns;\n+\n+\tns = val * NSEC_PER_SEC + val2;\n+\tint_time = find_closest_descending(\n+\t\t\tns,\n+\t\t\tmax44009_int_time_ns_array,\n+\t\t\tARRAY_SIZE(max44009_int_time_ns_array));\n+\n+\tret = i2c_smbus_read_byte_data(client, MAX44009_REG_CFG);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tconfig = ret;\n+\tconfig &= int_time;\n+\n+\t/*\n+\t * To set the integration time, the device must also be in manual\n+\t * mode.\n+\t */\n+\tconfig |= MAX44009_CFG_MAN_MODE_MASK;\n+\n+\treturn i2c_smbus_write_byte_data(client, MAX44009_REG_CFG, config);\n+}\n+\n+static int max44009_write_raw(struct iio_dev *indio_dev,\n+\t\t\t      struct iio_chan_spec const *chan, int val,\n+\t\t\t      int val2, long mask)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint ret;\n+\n+\tif (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {\n+\t\tmutex_lock(&data->lock);\n+\t\tret = max44009_write_int_time(data, val, val2);\n+\t\tmutex_unlock(&data->lock);\n+\t\treturn ret;\n+\t}\n+\treturn -EINVAL;\n+}\n+\n+static int max44009_write_raw_get_fmt(struct iio_dev *indio_dev,\n+\t\t\t\t      struct iio_chan_spec const *chan,\n+\t\t\t\t      long mask)\n+{\n+\treturn IIO_VAL_INT_PLUS_NANO;\n+}\n+\n+static int max44009_lux_raw(u8 hi, u8 lo)\n+{\n+\tint mantissa;\n+\tint exponent;\n+\n+\t/*\n+\t * The mantissa consists of the low nibble of the Lux High Byte\n+\t * and the low nibble of the Lux Low Byte.\n+\t */\n+\tmantissa = ((hi & 0xf) << 4) | (lo & 0xf);\n+\n+\t/* The exponent byte is just the upper nibble of the Lux High Byte */\n+\texponent = (hi >> 4) & 0xf;\n+\n+\t/*\n+\t * The exponent value is base 2 to the power of the raw exponent byte.\n+\t */\n+\texponent = 1 << exponent;\n+\n+\treturn exponent * mantissa;\n+}\n+\n+#define MAX44009_READ_LUX_XFER_LEN (4)\n+\n+static int max44009_read_lux_raw(struct max44009_data *data)\n+{\n+\tint ret;\n+\tu8 hireg = MAX44009_REG_LUX_HI;\n+\tu8 loreg = MAX44009_REG_LUX_LO;\n+\tu8 lo = 0;\n+\tu8 hi = 0;\n+\n+\tstruct i2c_msg msgs[] = {\n+\t\t{\n+\t\t\t.addr = data->client->addr,\n+\t\t\t.flags = 0,\n+\t\t\t.len = sizeof(hireg),\n+\t\t\t.buf = &hireg,\n+\t\t},\n+\t\t{\n+\t\t\t.addr = data->client->addr,\n+\t\t\t.flags = I2C_M_RD,\n+\t\t\t.len = sizeof(hi),\n+\t\t\t.buf = &hi,\n+\t\t},\n+\t\t{\n+\t\t\t.addr = data->client->addr,\n+\t\t\t.flags = 0,\n+\t\t\t.len = sizeof(loreg),\n+\t\t\t.buf = &loreg,\n+\t\t},\n+\t\t{\n+\t\t\t.addr = data->client->addr,\n+\t\t\t.flags = I2C_M_RD,\n+\t\t\t.len = sizeof(lo),\n+\t\t\t.buf = &lo,\n+\t\t}\n+\t};\n+\n+\t/*\n+\t * Use i2c_transfer instead of smbus read because i2c_transfer\n+\t * does NOT use a stop bit between address write and data read.\n+\t * Using a stop bit causes disjoint upper/lower byte reads and\n+\t * reduces accuracy.\n+\t */\n+\tret = i2c_transfer(data->client->adapter,\n+\t\t\t   msgs, MAX44009_READ_LUX_XFER_LEN);\n+\n+\tif (ret != MAX44009_READ_LUX_XFER_LEN)\n+\t\treturn -EIO;\n+\n+\treturn max44009_lux_raw(hi, lo);\n+}\n+\n+static int max44009_read_raw(struct iio_dev *indio_dev,\n+\t\t\t     struct iio_chan_spec const *chan, int *val,\n+\t\t\t     int *val2, long mask)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint lux_raw;\n+\tint ret;\n+\n+\tswitch (mask) {\n+\tcase IIO_CHAN_INFO_PROCESSED:\n+\t\tswitch (chan->type) {\n+\t\tcase IIO_LIGHT:\n+\t\t\tret = max44009_read_lux_raw(data);\n+\t\t\tif (ret < 0)\n+\t\t\t\treturn ret;\n+\t\t\tlux_raw = ret;\n+\n+\t\t\t*val = lux_raw * MAX44009_SCALE_NUMERATOR;\n+\t\t\t*val2 = MAX44009_SCALE_DENOMINATOR;\n+\t\t\treturn IIO_VAL_FRACTIONAL;\n+\t\tdefault:\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\tcase IIO_CHAN_INFO_INT_TIME:\n+\t\tswitch (chan->type) {\n+\t\tcase IIO_LIGHT:\n+\t\t\tret = max44009_read_int_time(data);\n+\t\t\tif (ret < 0)\n+\t\t\t\treturn ret;\n+\n+\t\t\t*val2 = ret;\n+\t\t\t*val = 0;\n+\t\t\treturn IIO_VAL_INT_PLUS_NANO;\n+\t\tdefault:\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\tdefault:\n+\t\treturn -EINVAL;\n+\t}\n+}\n+\n+static IIO_CONST_ATTR(illuminance_integration_time_available,\n+\t\t      max44009_int_time_str);\n+\n+static struct attribute *max44009_attributes[] = {\n+\t&iio_const_attr_illuminance_integration_time_available.dev_attr.attr,\n+\tNULL,\n+};\n+\n+static const struct attribute_group max44009_attribute_group = {\n+\t.attrs = max44009_attributes,\n+};\n+\n+static int max44009_threshold_byte_from_fraction(int integral, int fractional)\n+{\n+\tint mantissa, exp;\n+\n+\tif ((integral <= 0 && fractional <= 0) ||\n+\t     integral > MAX44009_MAXIMUM_THRESHOLD ||\n+\t     (integral == MAX44009_MAXIMUM_THRESHOLD && fractional != 0))\n+\t\treturn -EINVAL;\n+\n+\t/* Reverse scaling of fixed-point integral */\n+\tmantissa = integral * MAX44009_SCALE_DENOMINATOR;\n+\tmantissa /= MAX44009_SCALE_NUMERATOR;\n+\n+\t/* Reverse scaling of fixed-point fractional */\n+\tmantissa += fractional / MAX44009_FRACT_MULT *\n+\t\t    (MAX44009_SCALE_DENOMINATOR / MAX44009_SCALE_NUMERATOR);\n+\n+\tfor (exp = 0; mantissa > 0xff; exp++)\n+\t\tmantissa >>= 1;\n+\n+\tmantissa >>= 4;\n+\tmantissa &= 0xf;\n+\texp <<= 4;\n+\n+\treturn exp | mantissa;\n+}\n+\n+static int max44009_get_thr_reg(enum iio_event_direction dir)\n+{\n+\tswitch (dir) {\n+\tcase IIO_EV_DIR_RISING:\n+\t\treturn MAX44009_REG_UPPER_THR;\n+\tcase IIO_EV_DIR_FALLING:\n+\t\treturn MAX44009_REG_LOWER_THR;\n+\tdefault:\n+\t\treturn -EINVAL;\n+\t}\n+}\n+\n+static int max44009_write_event_value(struct iio_dev *indio_dev,\n+\t\t\t\t      const struct iio_chan_spec *chan,\n+\t\t\t\t      enum iio_event_type type,\n+\t\t\t\t      enum iio_event_direction dir,\n+\t\t\t\t      enum iio_event_info info,\n+\t\t\t\t      int val, int val2)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint reg, threshold;\n+\n+\tif (info != IIO_EV_INFO_VALUE || chan->type != IIO_LIGHT)\n+\t\treturn -EINVAL;\n+\n+\tthreshold = max44009_threshold_byte_from_fraction(val, val2);\n+\tif (threshold < 0)\n+\t\treturn threshold;\n+\n+\treg = max44009_get_thr_reg(dir);\n+\tif (reg < 0)\n+\t\treturn reg;\n+\n+\treturn i2c_smbus_write_byte_data(data->client, reg, threshold);\n+}\n+\n+static int max44009_read_threshold(struct iio_dev *indio_dev,\n+\t\t\t\t   enum iio_event_direction dir)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint byte, reg;\n+\tint mantissa, exponent;\n+\n+\treg = max44009_get_thr_reg(dir);\n+\tif (reg < 0)\n+\t\treturn reg;\n+\n+\tbyte = i2c_smbus_read_byte_data(data->client, reg);\n+\tif (byte < 0)\n+\t\treturn byte;\n+\n+\tmantissa = byte & MAX44009_THRESH_MANT_MASK;\n+\tmantissa <<= MAX44009_THRESH_MANT_LSHIFT;\n+\n+\t/*\n+\t * To get the upper threshold, always adds the minimum upper threshold\n+\t * value to the shifted byte value (see datasheet).\n+\t */\n+\tif (dir == IIO_EV_DIR_RISING)\n+\t\tmantissa += MAX44009_UPPER_THR_MINIMUM;\n+\n+\t/*\n+\t * Exponent is base 2 to the power of the threshold exponent byte\n+\t * value\n+\t */\n+\texponent = byte & MAX44009_THRESH_EXP_MASK;\n+\texponent >>= MAX44009_THRESH_EXP_RSHIFT;\n+\n+\treturn (1 << exponent) * mantissa;\n+}\n+\n+static int max44009_read_event_value(struct iio_dev *indio_dev,\n+\t\t\t\t     const struct iio_chan_spec *chan,\n+\t\t\t\t     enum iio_event_type type,\n+\t\t\t\t     enum iio_event_direction dir,\n+\t\t\t\t     enum iio_event_info info,\n+\t\t\t\t     int *val, int *val2)\n+{\n+\tint ret;\n+\tint threshold;\n+\n+\tif (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)\n+\t\treturn -EINVAL;\n+\n+\tret = max44009_read_threshold(indio_dev, dir);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\tthreshold = ret;\n+\n+\t*val = threshold * MAX44009_SCALE_NUMERATOR;\n+\t*val2 = MAX44009_SCALE_DENOMINATOR;\n+\n+\treturn IIO_VAL_FRACTIONAL;\n+}\n+\n+static int max44009_write_event_config(struct iio_dev *indio_dev,\n+\t\t\t\t       const struct iio_chan_spec *chan,\n+\t\t\t\t       enum iio_event_type type,\n+\t\t\t\t       enum iio_event_direction dir,\n+\t\t\t\t       int state)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint ret;\n+\n+\tif (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)\n+\t\treturn -EINVAL;\n+\n+\tret = i2c_smbus_write_byte_data(data->client,\n+\t\t\t\t\tMAX44009_REG_INT_EN, state);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Set device to trigger interrupt immediately upon exceeding\n+\t * the threshold limit.\n+\t */\n+\treturn i2c_smbus_write_byte_data(data->client,\n+\t\t\t\t\t MAX44009_REG_THR_TIMER, 0);\n+}\n+\n+static int max44009_read_event_config(struct iio_dev *indio_dev,\n+\t\t\t\t      const struct iio_chan_spec *chan,\n+\t\t\t\t      enum iio_event_type type,\n+\t\t\t\t      enum iio_event_direction dir)\n+{\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\n+\tif (chan->type != IIO_LIGHT || type != IIO_EV_TYPE_THRESH)\n+\t\treturn -EINVAL;\n+\n+\treturn i2c_smbus_read_byte_data(data->client, MAX44009_REG_INT_EN);\n+}\n+\n+static const struct iio_info max44009_info = {\n+\t.read_raw = max44009_read_raw,\n+\t.write_raw = max44009_write_raw,\n+\t.write_raw_get_fmt = max44009_write_raw_get_fmt,\n+\t.read_event_value = max44009_read_event_value,\n+\t.read_event_config = max44009_read_event_config,\n+\t.write_event_value = max44009_write_event_value,\n+\t.write_event_config = max44009_write_event_config,\n+\t.attrs = &max44009_attribute_group,\n+};\n+\n+static irqreturn_t max44009_threaded_irq_handler(int irq, void *p)\n+{\n+\tstruct iio_dev *indio_dev = p;\n+\tstruct max44009_data *data = iio_priv(indio_dev);\n+\tint ret;\n+\n+\tret = i2c_smbus_read_byte_data(data->client, MAX44009_REG_INT_STATUS);\n+\tif (ret) {\n+\t\tiio_push_event(indio_dev,\n+\t\t\t       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,\n+\t\t\t\t\t\t    IIO_EV_TYPE_THRESH,\n+\t\t\t\t\t\t    IIO_EV_DIR_EITHER),\n+\t\t\t       iio_get_time_ns(indio_dev));\n+\n+\t\treturn IRQ_HANDLED;\n+\t}\n+\n+\treturn IRQ_NONE;\n+}\n+\n+static int max44009_probe(struct i2c_client *client,\n+\t\t\t  const struct i2c_device_id *id)\n+{\n+\tstruct max44009_data *data;\n+\tstruct iio_dev *indio_dev;\n+\tint ret;\n+\n+\tindio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));\n+\tif (!indio_dev)\n+\t\treturn -ENOMEM;\n+\n+\tdata = iio_priv(indio_dev);\n+\ti2c_set_clientdata(client, indio_dev);\n+\tdata->client = client;\n+\tindio_dev->dev.parent = &client->dev;\n+\tindio_dev->info = &max44009_info;\n+\tindio_dev->modes = INDIO_DIRECT_MODE;\n+\tindio_dev->name = MAX44009_DRV_NAME;\n+\tindio_dev->channels = max44009_channels;\n+\tindio_dev->num_channels = ARRAY_SIZE(max44009_channels);\n+\tmutex_init(&data->lock);\n+\n+\t/* Clear any stale interrupt bit */\n+\tret = i2c_smbus_read_byte_data(client, MAX44009_REG_CFG);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tif (client->irq > 0) {\n+\t\tret = devm_request_threaded_irq(&client->dev, client->irq,\n+\t\t\t\t\t\tNULL,\n+\t\t\t\t\t\tmax44009_threaded_irq_handler,\n+\t\t\t\t\t\tIRQF_TRIGGER_FALLING |\n+\t\t\t\t\t\tIRQF_ONESHOT | IRQF_SHARED,\n+\t\t\t\t\t\t\"max44009_event\",\n+\t\t\t\t\t\tindio_dev);\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\t}\n+\n+\treturn devm_iio_device_register(&client->dev, indio_dev);\n+}\n+\n+static const struct i2c_device_id max44009_id[] = {\n+\t{ \"max44009\", 0 },\n+\t{ }\n+};\n+MODULE_DEVICE_TABLE(i2c, max44009_id);\n+\n+static struct i2c_driver max44009_driver = {\n+\t.driver = {\n+\t\t.name = MAX44009_DRV_NAME,\n+\t},\n+\t.probe = max44009_probe,\n+\t.id_table = max44009_id,\n+};\n+module_i2c_driver(max44009_driver);\n+\n+static const struct of_device_id max44009_of_match[] = {\n+\t{ .compatible = \"maxim,max44009\" },\n+\t{ }\n+};\n+MODULE_DEVICE_TABLE(of, max44009_of_match);\n+\n+MODULE_AUTHOR(\"Robert Eshleman <bobbyeshleman@gmail.com>\");\n+MODULE_LICENSE(\"GPL v2\");\n+MODULE_DESCRIPTION(\"MAX44009 ambient light sensor driver\");\n",
    "prefixes": [
        "v3",
        "1/2"
    ]
}