/*
 * Copyright (c) 2015, Zolertia - http://www.zolertia.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
/*---------------------------------------------------------------------------*/
/**
 * \addtogroup zoul-tsl2563-sensor
 * @{
 *
 * \file
 *  Driver for the external TSL2563 light sensor
 *
 * \author
 *         Antonio Lignan <alinan@zolertia.com>
 *         Toni Lozano <tlozano@zolertia.com>
 */
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/i2c.h"
#include "dev/gpio.h"
#include "dev/zoul-sensors.h"
#include "lib/sensors.h"
#include "tsl2563.h"
/*---------------------------------------------------------------------------*/
#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/
#define TSL2563_INT_PORT_BASE  GPIO_PORT_TO_BASE(I2C_INT_PORT)
#define TSL2563_INT_PIN_MASK   GPIO_PIN_MASK(I2C_INT_PIN)
/*---------------------------------------------------------------------------*/
static uint8_t enabled;
static uint8_t gain;
static uint8_t timming;
/*---------------------------------------------------------------------------*/
void (*tsl2563_int_callback)(uint8_t value);
/*---------------------------------------------------------------------------*/
static uint16_t
calculate_lux(uint8_t *buf)
{
  uint32_t ch0, ch1, chscale = 0;
  uint32_t ratio = 0;
  uint32_t lratio, tmp = 0;
  uint16_t buffer[2];

  /* The calculations below assume the integration time is 402ms and the gain
   * is 16x (nominal), if not then it is required to normalize the reading
   * before converting to lux
   */

  buffer[0] = (buf[1] << 8 | (buf[0]));
  buffer[1] = (buf[3] << 8 | (buf[2]));

  switch(timming) {
  case TSL2563_TIMMING_INTEG_402MS:
    chscale = (1 << CH_SCALE);
    break;
  case TSL2563_TIMMING_INTEG_101MS:
    chscale = CHSCALE_TINT1;
    break;
  case TSL2563_TIMMING_INTEG_13_7MS:
    chscale = CHSCALE_TINT0;
    break;
  }

  if(!gain) {
    chscale = chscale << 4;
  }

  ch0 = (buffer[0] * chscale) >> CH_SCALE;
  ch1 = (buffer[1] * chscale) >> CH_SCALE;

  if(ch0 > 0) {
    ratio = (ch1 << CH_SCALE);
    ratio = ratio / ch0;
  }

  lratio = (ratio + 1) >> 1;

  if((lratio >= 0) && (lratio <= K1T)) {
    tmp = (ch0 * B1T) - (ch1 * M1T);
  } else if(lratio <= K2T) {
    tmp = (ch0 * B2T) - (ch1 * M2T);
  } else if(lratio <= K3T) {
    tmp = (ch0 * B3T) - (ch1 * M3T);
  } else if(lratio <= K4T) {
    tmp = (ch0 * B4T) - (ch1 * M4T);
  } else if(lratio <= K5T) {
    tmp = (ch0 * B5T) - (ch1 * M5T);
  } else if(lratio <= K6T) {
    tmp = (ch0 * B6T) - (ch1 * M6T);
  } else if(lratio <= K7T) {
    tmp = (ch0 * B7T) - (ch1 * M7T);
  } else if(lratio > K8T) {
    tmp = (ch0 * B8T) - (ch1 * M8T);
  }

  if(tmp < 0) {
    tmp = 0;
  }

  tmp += (1 << (LUX_SCALE - 1));
  return tmp >> LUX_SCALE;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_read_reg(uint8_t reg, uint8_t *buf, uint8_t regNum)
{
  i2c_master_enable();
  if(i2c_single_send(TSL2563_ADDR, reg) == I2C_MASTER_ERR_NONE) {
    while(i2c_master_busy());
    if(i2c_burst_receive(TSL2563_ADDR, buf, regNum) == I2C_MASTER_ERR_NONE) {
      return TSL2563_SUCCESS;
    }
  }
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_write_reg(uint8_t *buf, uint8_t num)
{
  if((buf == NULL) || (num <= 0)) {
    PRINTF("TSL2563: invalid write values\n");
    return TSL2563_ERROR;
  }

  i2c_master_enable();
  if(i2c_burst_send(TSL2563_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
    return TSL2563_SUCCESS;
  }
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_on(void)
{
  uint8_t buf[2];
  buf[0] = (TSL2563_COMMAND + TSL2563_CONTROL);
  buf[1] = TSL2563_CONTROL_POWER_ON;

  if(tsl2563_write_reg(buf, 2) == I2C_MASTER_ERR_NONE) {
    if(i2c_single_receive(TSL2563_ADDR, &buf[0]) == I2C_MASTER_ERR_NONE) {
      if((buf[0] & 0x0F) == TSL2563_CONTROL_POWER_ON) {
        PRINTF("TSL2563: powered on\n");
        return TSL2563_SUCCESS;
      }
    }
  }

  PRINTF("TSL2563: failed to power on\n");
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_id_register(uint8_t *buf)
{
  if(tsl2563_read_reg((TSL2563_COMMAND + TSL2563_ID_REG),
                      buf, 1) == TSL2563_SUCCESS) {
    PRINTF("TSL2563: partnum/revnum 0x%02X\n", *buf);
    return TSL2563_SUCCESS;
  }

  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_off(void)
{
  uint8_t buf[2];
  buf[0] = (TSL2563_COMMAND + TSL2563_CONTROL);
  buf[1] = TSL2563_CONTROL_POWER_OFF;

  if(tsl2563_write_reg(buf, 2) == I2C_MASTER_ERR_NONE) {
    PRINTF("TSL2563: powered off\n");
    return TSL2563_SUCCESS;
  }

  PRINTF("TSL2563: failed to power off\n");
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_clear_interrupt(void)
{
  uint8_t buf = (TSL2563_COMMAND + TSL2563_CLEAR_INTERRUPT);
  if(tsl2563_write_reg(&buf, 1) != I2C_MASTER_ERR_NONE) {
    PRINTF("TSL2563: failed to clear the interrupt\n");
    return TSL2563_ERROR;
  }
  return TSL2563_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static int
tsl2563_read_sensor(uint16_t *lux)
{
  uint8_t buf[4];

  /* This is hardcoded to use word write/read operations */
  if(tsl2563_read_reg((TSL2563_COMMAND + TSL2563_D0LOW),
                      &buf[0], 2) == TSL2563_SUCCESS) {
    if(tsl2563_read_reg((TSL2563_COMMAND + TSL2563_D1LOW),
                        &buf[2], 2) == TSL2563_SUCCESS) {

      PRINTF("TSL2563: CH0 0x%02X%02X CH1 0x%02X%02X\n", buf[1], buf[0],
             buf[3], buf[2]);
      *lux = calculate_lux(buf);
      return TSL2563_SUCCESS;
    }
  }
  PRINTF("TSL2563: failed to read\n");
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
PROCESS(tsl2563_int_process, "TSL2563 interrupt process handler");
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(tsl2563_int_process, ev, data)
{
  PROCESS_EXITHANDLER();
  PROCESS_BEGIN();

  while(1) {
    PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
    tsl2563_clear_interrupt();
    tsl2563_int_callback(0);
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
static void
tsl2563_interrupt_handler(uint8_t port, uint8_t pin)
{
  /* There's no alert/interruption flag to check, clear the interruption by
   * writting to the CLEAR bit in the COMMAND register
   */
  process_poll(&tsl2563_int_process);
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int value)
{
  uint8_t buf[3];

  if((type != TSL2563_ACTIVE) && (type != TSL2563_INT_OVER) &&
     (type != TSL2563_INT_BELOW) && (type != TSL2563_INT_DISABLE) &&
     (type != TSL2563_TIMMING_CFG)) {
    PRINTF("TSL2563: invalid start value\n");
    return TSL2563_ERROR;
  }

  /* As default the power-on values of the sensor are gain 1X, 402ms integration
   * time (not nominal), with manual control disabled
   */

  if(type == TSL2563_ACTIVE) {
    if(value) {
      i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
               I2C_SCL_NORMAL_BUS_SPEED);

      /*  Initialize interrupts handlers */
      tsl2563_int_callback = NULL;

      /* Power on the sensor and check for the part number */
      if(tsl2563_on() == TSL2563_SUCCESS) {
        if(tsl2563_id_register(&buf[0]) == TSL2563_SUCCESS) {
          if((buf[0] & TSL2563_ID_PARTNO_MASK) == TSL2563_EXPECTED_PARTNO) {

            /* Read the timming/gain configuration */
            if(tsl2563_read_reg((TSL2563_COMMAND + TSL2563_TIMMING),
                                &buf[0], 1) == TSL2563_SUCCESS) {
              gain = buf[0] & TSL2563_TIMMING_GAIN;
              timming = buf[0] & TSL2563_TIMMING_INTEG_MASK;
              PRINTF("TSL2563: enabled, timming %u gain %u\n", timming, gain);

              /* Restart the over interrupt threshold */
              buf[0] = (TSL2563_COMMAND + TSL2563_THRHIGHLOW);
              buf[1] = 0xFF;
              buf[2] = 0xFF;

              if(tsl2563_write_reg(buf, 3) != TSL2563_SUCCESS) {
                PRINTF("TSL2563: failed to clear over interrupt\n");
                return TSL2563_ERROR;
              }

              /* Restart the below interrupt threshold */
              buf[0] = (TSL2563_COMMAND + TSL2563_THRLOWLOW);
              buf[1] = 0x00;
              buf[2] = 0x00;

              if(tsl2563_write_reg(buf, 3) != TSL2563_SUCCESS) {
                PRINTF("TSL2563: failed to clear below interrupt\n");
                return TSL2563_ERROR;
              }

              /* Clear any pending interrupt */
              if(tsl2563_clear_interrupt() == TSL2563_SUCCESS) {
                enabled = 1;
                return TSL2563_SUCCESS;
              }
            }
          }
        }
      }
      return TSL2563_ERROR;
    } else {
      if(tsl2563_off() == TSL2563_SUCCESS) {
        PRINTF("TSL2563: stopped\n");
        enabled = 0;
        return TSL2563_SUCCESS;
      }
      return TSL2563_ERROR;
    }
  }

  if(!enabled) {
    PRINTF("TSL2563: sensor not started\n");
    return TSL2563_ERROR;
  }

  if(type == TSL2563_INT_DISABLE) {

    /* Ensure the GPIO doesn't generate more interrupts, this may affect others
     * I2C digital sensors using the bus and sharing this pin, so an user may
     * comment the line below
     */
    GPIO_DISABLE_INTERRUPT(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);

    /* This also wipes out the persistance value, to be reconfigured when
     * enabling back the interruption
     */
    buf[0] = (TSL2563_COMMAND + TSL2563_INTERRUPT);
    buf[1] = TSL2563_INTR_DISABLED;

    if(tsl2563_write_reg(buf, 2) != TSL2563_SUCCESS) {
      PRINTF("TSL2563: failed to disable the interrupt\n");
      return TSL2563_ERROR;
    }
    return TSL2563_SUCCESS;
  }

  /* Configure the timming and gain */
  if(type == TSL2563_TIMMING_CFG) {
    if((value != TSL2563_G16X_402MS) && (value != TSL2563_G1X_402MS) &&
       (value != TSL2563_G1X_101MS) && (value != TSL2563_G1X_13_7MS)) {
      PRINTF("TSL2563: invalid timming configuration values\n");
      return TSL2563_ERROR;
    }

    buf[0] = (TSL2563_COMMAND + TSL2563_TIMMING);
    buf[1] = value;

    if(tsl2563_write_reg(buf, 2) == TSL2563_SUCCESS) {
      if(value == TSL2563_G16X_402MS) {
        gain = 1;
      }

      switch(value) {
      case TSL2563_G16X_402MS:
      case TSL2563_G1X_402MS:
        timming = TSL2563_TIMMING_INTEG_402MS;
        break;
      case TSL2563_G1X_101MS:
        timming = TSL2563_TIMMING_INTEG_101MS;
        break;
      case TSL2563_G1X_13_7MS:
        timming = TSL2563_TIMMING_INTEG_13_7MS;
        break;
      }

      PRINTF("TSL2563: new timming %u gain %u\n", timming, gain);
      return TSL2563_SUCCESS;
    }
    PRINTF("TSL2563: failed to configure timming\n");
    return TSL2563_ERROR;
  }

  /* From here we handle the interrupt configuration, it requires the interrupt
   * callback handler to have been previously set using the TSL2563_REGISTER_INT
   * macro
   */

  buf[1] = ((uint8_t *)&value)[0];
  buf[2] = ((uint8_t *)&value)[1];

  if(type == TSL2563_INT_OVER) {
    buf[0] = (TSL2563_COMMAND + TSL2563_THRHIGHLOW);
  } else if(type == TSL2563_INT_BELOW) {
    buf[0] = (TSL2563_COMMAND + TSL2563_THRLOWLOW);
  }

  if(tsl2563_write_reg(buf, 3) != TSL2563_SUCCESS) {
    PRINTF("TSL2563: failed to set interrupt level\n");
    return TSL2563_ERROR;
  }

  /* Now configure the interruption register (level interrupt, 2 integration
   * cycles after threshold has been reached (roughly 804ms if timming is 402ms)
   */
  buf[0] = (TSL2563_COMMAND + TSL2563_INTERRUPT);
  buf[1] = (TSL2563_INTR_LEVEL << TSL2563_INTR_SHIFT);
  buf[1] += TSL2563_INT_PERSIST_2_CYCLES;

  if(tsl2563_write_reg(buf, 2) != TSL2563_SUCCESS) {
    PRINTF("TSL2563: failed to enable interrupt\n");
    return TSL2563_ERROR;
  }

  /* Configure the interrupts pins */
  GPIO_SOFTWARE_CONTROL(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
  GPIO_SET_INPUT(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);

  /* Pull-up resistor, detect falling edge */
  GPIO_DETECT_EDGE(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
  GPIO_TRIGGER_SINGLE_EDGE(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
  GPIO_DETECT_FALLING(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
  gpio_register_callback(tsl2563_interrupt_handler, I2C_INT_PORT, I2C_INT_PIN);

  /* Spin process until an interrupt is received */
  process_start(&tsl2563_int_process, NULL);

  /* Enable interrupts */
  GPIO_ENABLE_INTERRUPT(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);

  /* The RE-Mote revision A has this pin shared and with a pull-down resistor,
   * for other platforms (like the firefly), change to enable pull-up internal
   * resistor instead if no external pull-up is present.
   */
  ioc_set_over(I2C_INT_PORT, I2C_INT_PIN, IOC_OVERRIDE_PUE);
  nvic_interrupt_enable(I2C_INT_VECTOR);

  PRINTF("TSL2563: Interrupt configured\n");
  return TSL2563_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
  switch(type) {
  case SENSORS_ACTIVE:
  case SENSORS_READY:
    return enabled;
  }
  return 0;
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
  uint16_t lux;

  if(!enabled) {
    PRINTF("TSL2563: sensor not started\n");
    return TSL2563_ERROR;
  }

  if(type == TSL2563_VAL_READ) {
    if(tsl2563_read_sensor(&lux) != TSL2563_ERROR) {
      return lux;
    }
    PRINTF("TSL2563: fail to read\n");
  }
  return TSL2563_ERROR;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(tsl2563, TSL2563_SENSOR, value, configure, status);
/*---------------------------------------------------------------------------*/
/** @} */