示例#1
0
/**
 * Main function
 * Initialize controller and handle Modbus requests
**/
int main(void) {
	uint8_t i;

	// load config data from eeprom
	loadEepromValues();

	// init modules
	led_init();
	adc_init();
	windspeed_init(&windspeed, &errcnt, &cnt);
	onewiretemp_init();
	mb_init();
	mb_setIP(config.ip);

	// register adc handlers
	adc_register(0, read_temperature);
	adc_register(1, read_winddir);

	// register Modbus handlers and registers
	mb_addReadRegister(ENABLENUM, &(config.enable));
	mb_addReadRegister(3, (uint16_t*)&winddir);
	mb_addReadRegister(4, (uint16_t*)&windspeed);
	mb_addReadRegister(5, (uint16_t*)&temperature);
	mb_addReadRegister(6, (uint16_t*)&errcnt);
	mb_addReadRegister(7, (uint16_t*)&cnt);
	mb_addReadRegister(8, (uint16_t*)&insideTemperature);

	mb_addWriteRegister(IP_HIGH, write_IP);
	mb_addWriteRegister(IP_LOW, write_IP);
	mb_addWriteRegister(ENABLENUM, write_enable);
	for (i = TEMPNUM; i < TEMPNUM+2*ARRAY_SIZE(config.temperatureCalibration); i++)
		mb_addWriteRegister(i, write_temperatureCalibration);

	// set DDR for sensor on/off
	SENS_DDR |= _BV(SENS_PIN);

	// enable interrupts
	sei();

	// start proccessing
	setSensorStatus();

	while (1) {
		// apply new IP address if requested
		if (renewIP) {
			renewIP = 0;
			mb_setIP(config.ip);
		}
		mb_handleRequest();
		onewiretemp_update(&insideTemperature);
	}
}
示例#2
0
文件: lpc17_adc.c 项目: a1ien/nuttx
int mbed_adc_setup(void)
{
    static bool initialized = false;
    struct adc_dev_s *adc;
    int ret;

    /* Check if we have already initialized */

    if (!initialized)
    {
        /* Call lpc17_adcinitialize() to get an instance of the ADC interface */

        adc = lpc17_adcinitialize();
        if (adc == NULL)
        {
            aerr("ERROR: Failed to get ADC interface\n");
            return -ENODEV;
        }

        /* Register the ADC driver at "/dev/adc0" */

        ret = adc_register("/dev/adc0", adc);
        if (ret < 0)
        {
            aerr("ERROR: adc_register failed: %d\n", ret);
            return ret;
        }

        /* Now we are initialized */

        initialized = true;
    }

    return OK;
}
示例#3
0
int stm32_adc_setup(void)
{
  static bool initialized = false;
  uint8_t channel[1] = {10};
  struct adc_dev_s *adc;
  int rv;

  if (initialized)
    {
      return OK;
    }

  ainfo("INFO: Initializing ADC12_IN10\n");
  stm32_configgpio(GPIO_ADC12_IN10);
  if ((adc = stm32_adcinitialize(1, channel, 1)) == NULL)
    {
      aerr("ERROR: Failed to get adc interface\n");
      return -ENODEV;
    }

  if ((rv = adc_register("/dev/adc0", adc)) < 0)
    {
      aerr("ERROR: adc_register failed: %d\n", rv);
      return rv;
    }

  initialized = true;
  ainfo("INFO: ADC12_IN10 initialized succesfully\n");
  return OK;
}
示例#4
0
static int rk29_lsr_adc_init(struct platform_device *dev)
{
	struct rk29_lsr_platform_data *pdata = dev->dev.platform_data;
	pdata->client = adc_register(pdata->adc_chn, callback, "lsr_adc");
	if(!pdata->client)
		return -EINVAL;
	mutex_init(&pdata->lsr_mutex);
	return 0;
}
示例#5
0
int board_gsensoradc_setup(void)
{
#ifdef CONFIG_STM32_ADC1
  
  static bool initialized = false;
  struct adc_dev_s *adc;
  int ret;
  int i;

  /* Check if we have already initialized */
  
  if (!initialized){
      /* Configure the pins as analog inputs for the selected channels */

    for (i = 0; i < ADC1_NCHANNELS; i++)
    {
      stm32_configgpio(g_pinlist[i]);
      //stm32_configgpio(g_detect[i]);
    }

      /* Call stm32_adcinitialize() to get an instance of the ADC interface */

    adc = stm32_adcinitialize(1, g_chanlist, ADC1_NCHANNELS);

    if (adc == NULL)
    {
      adbg("ERROR: Failed to get ADC interface\n");
      return -ENODEV;
    }


      /* Register the ADC driver at "/dev/adc0" */

    ret = adc_register("/dev/gsensor", adc);
    if (ret < 0)
    {
      adbg("adc_register failed: %d\n", ret);
      return ret;
    }

      /* Now we are initialized */

    initialized = true;
  }

  return OK;
#else
  return -ENOSYS;
#endif
}
static int rk29_adc_test(void)
{
	struct adc_test_data *test = NULL;

	test = kzalloc(sizeof(struct adc_test_data), GFP_KERNEL);
	
	test->client = adc_register(0, callback, NULL);
	INIT_WORK(&test->timer_work, adc_timer_work);
	setup_timer(&test->timer, adc_timer, (unsigned long)test);
	test->timer.expires  = jiffies + 100;
	add_timer(&test->timer);
	
	return 0;

}
示例#7
0
int adc_devinit(void)
{
	static bool initialized = false;
	struct adc_dev_s *adc[ADC3_NCHANNELS];
	int ret;
	int i;

	/* Check if we have already initialized */

	if (!initialized) {
		char name[11];

		for (i = 0; i < ADC3_NCHANNELS; i++) {
			stm32_configgpio(g_pinlist[i]);
		}

		for (i = 0; i < 1; i++) {
			/* Configure the pins as analog inputs for the selected channels */
			//stm32_configgpio(g_pinlist[i]);

			/* Call stm32_adcinitialize() to get an instance of the ADC interface */
			//multiple channels only supported with dma!
			adc[i] = stm32_adcinitialize(3, (g_chanlist), 4);

			if (adc == NULL) {
				adbg("ERROR: Failed to get ADC interface\n");
				return -ENODEV;
			}


			/* Register the ADC driver at "/dev/adc0" */
			sprintf(name, "/dev/adc%d", i);
			ret = adc_register(name, adc[i]);

			if (ret < 0) {
				adbg("adc_register failed for adc %s: %d\n", name, ret);
				return ret;
			}
		}

		/* Now we are initialized */

		initialized = true;
	}

	return OK;
}
示例#8
0
static int rk30_adc_test(void)
{
        int i;
	struct adc_test_data *test[CHN_NR];

        adc_wq = create_singlethread_workqueue("adc_test");
	for(i = 0; i < CHN_NR; i++){
	        test[i] = kzalloc(sizeof(struct adc_test_data), GFP_KERNEL);
	        test[i]->client = adc_register(i, callback, NULL);
	        INIT_WORK(&test[i]->timer_work, adc_timer_work);
	        setup_timer(&test[i]->timer, adc_timer, (unsigned long)test[i]);
                mod_timer(&test[i]->timer, jiffies+msecs_to_jiffies(20));
        }
	
	return 0;

}
示例#9
0
bool iotjs_adc_open(iotjs_adc_t* adc) {
  iotjs_adc_platform_data_t* platform_data = adc->platform_data;

  uint32_t pin = platform_data->pin;
  int32_t adc_number = ADC_GET_NUMBER(pin);
  int32_t timer = SYSIO_GET_TIMER(pin);
  struct adc_dev_s* adc_dev = iotjs_adc_config_nuttx(adc_number, timer, pin);

  char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
  adc_get_path(path, adc_number);

  if (adc_register(path, adc_dev) != 0) {
    return false;
  }

  DDDLOG("%s - path: %s, number: %d, timer: %d", __func__, path, adc_number,
         timer);

  return true;
}
示例#10
0
static void c5_lcd_detect()
{
	if (board_type != BOARD_C5)
		return;

	struct adc_client	*client;
	client = adc_register(2, wld_adc_callback, NULL);

	if (!client) {
		printk("adc_register failed\n");
		return;
	}
			
	int adc_val = adc_sync_read(client);
	if (adc_val < 10) {
		panel_type = PANEL_HX8394;	
	} else {
		panel_type = PANEL_MQ0801D;
	}
}
示例#11
0
int adc_devinit(void)
{
#ifdef CONFIG_SAMA5_ADC
  static bool initialized = false;
  struct adc_dev_s *adc;
  int ret;

  /* Check if we have already initialized */

  if (!initialized)
    {
      /* Call stm32_adcinitialize() to get an instance of the ADC interface */

      adc = sam_adc_initialize();
      if (adc == NULL)
        {
          adbg("ERROR: Failed to get ADC interface\n");
          return -ENODEV;
        }

      /* Register the ADC driver at "/dev/adc0" */

      ret = adc_register("/dev/adc0", adc);
      if (ret < 0)
        {
          adbg("adc_register failed: %d\n", ret);
          return ret;
        }

      /* Now we are initialized */

      initialized = true;
    }

  return OK;
#else
  return -ENOSYS;
#endif
}
示例#12
0
/** Battery Monitor Initialization.
 *
 * When this function is called the driver probes and installs the
 * battery monitor driver.
 */
static int rk29_adc_battery_probe(struct platform_device *pdev)
{
	int ret;
	int irq;
	int irq_flag;
	struct adc_client *client;
	struct rk29_adc_battery_data *data;
	struct rk29_adc_battery_platform_data *pdata = pdev->dev.platform_data;

	printk("%s--%d:\n", __FUNCTION__, __LINE__);
	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (data == NULL)
	{
		ret = -ENOMEM;
		goto err_data_alloc_failed;
	}
	gBatteryData = data;
	platform_set_drvdata(pdev, data);

	ret = rk29_adc_battery_io_init(data, pdata);
	if (ret)
	{
		goto err_io_init;
	}

	/* Register ADC memory for battery sampling and mean value calc. */
	memset(data->adc_samples, 0, sizeof(int) * (NUM_VOLTAGE_SAMPLE + 2));
	client = adc_register(0, rk29_adc_battery_callback, NULL);
	if (!client)
		goto err_adc_register_failed;

	//variable init
	data->client = client;
	data->adc_val = adc_sync_read(client);

	/* Setup timers for ADC and battery work queues. */
	setup_timer(&data->timer, rk29_adc_battery_scan_timer, (unsigned long)data);
	data->timer.expires = jiffies + 2000; /* First start delayed by 2s to finish init. */
	add_timer(&data->timer);

	INIT_WORK(&data->timer_work, rk29_adc_battery_timer_work);
	INIT_WORK(&data->resume_work, rk29_adc_battery_resume_check);

#if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
	//init dc dectet irq & delay work
	if (pdata->dc_det_pin != INVALID_GPIO)
	{
		irq = gpio_to_irq(pdata->dc_det_pin);

		irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
		ret = request_irq(irq, rk29_adc_battery_dc_wakeup, irq_flag, "rk29_adc_battery", NULL);
		if (ret)
		{
			printk("failed to request dc det irq\n");
			goto err_dcirq_failed;
		}
		enable_irq_wake(irq);

		INIT_WORK(&data->dcwakeup_work, rk29_adc_battery_dcdet_delaywork);
	}
#endif

	/* Power on Battery detection. */
	rk29_adc_battery_lowpower_check(data);

	/* Power supply register. */
	wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");

	ret = power_supply_register(&pdev->dev, &rk29_battery_supply);
	if (ret)
	{
		printk(KERN_INFO "fail to battery power_supply_register\n");
		goto err_battery_failed;
	}

#if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
	ret = power_supply_register(&pdev->dev, &rk29_ac_supply);
	if (ret)
	{
		printk(KERN_INFO "fail to ac power_supply_register\n");
		goto err_ac_failed;
	}
#endif

#if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
	ret = power_supply_register(&pdev->dev, &rk29_usb_supply);
	if (ret)
	{
		printk(KERN_INFO "fail to usb power_supply_register\n");
		goto err_usb_failed;
	}
#endif

	printk(KERN_INFO "rk29_adc_battery: driver initialized\n");

	return 0;

#if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
	err_usb_failed:
	power_supply_unregister(&rk29_usb_supply);
#endif

	err_ac_failed:
#if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
	power_supply_unregister(&rk29_ac_supply);
#endif

err_battery_failed:
	power_supply_unregister(&rk29_battery_supply);

err_dcirq_failed:
	free_irq(gpio_to_irq(pdata->dc_det_pin), data);

err_adc_register_failed:
err_io_init:
err_data_alloc_failed:
	kfree(data);

	printk("rk29_adc_battery: error!\n");

	return ret;
}
static int rockchip_headsetobserve_probe(struct platform_device *pdev)
{
	int ret;
	struct headset_priv *headset;
	struct rk_headset_pdata *pdata;

	headset = kzalloc(sizeof(struct headset_priv), GFP_KERNEL);
	if (headset == NULL) {
		dev_err(&pdev->dev, "failed to allocate driver data\n");
		return -ENOMEM;
	}
	headset_info = headset;
	headset->pdata = pdev->dev.platform_data;
	pdata = headset->pdata;
	headset->headset_status = HEADSET_OUT;
	headset->heatset_irq_working = IDLE;
	headset->hook_status = HOOK_UP;
	headset->hook_time = HOOK_ADC_SAMPLE_TIME;
	headset->cur_headset_status = 0;
	headset->sdev.name = "h2w";
	headset->sdev.print_name = h2w_print_name;
	ret = switch_dev_register(&headset->sdev);
	if (ret < 0)
		goto failed_free;
	
//	mutex_init(&headset->mutex_lock[HEADSET]);
//	mutex_init(&headset->mutex_lock[HOOK]);
	wake_lock_init(&headset->headset_on_wake, WAKE_LOCK_SUSPEND, "headset_on_wake");
	INIT_DELAYED_WORK(&headset->h_delayed_work[HEADSET], headsetobserve_work);

	headset->isMic = 0;
//------------------------------------------------------------------		
	// Create and register the input driver. 
	headset->input_dev = input_allocate_device();
	if (!headset->input_dev) {
		dev_err(&pdev->dev, "failed to allocate input device\n");
		ret = -ENOMEM;
		goto failed_free;
	}	
	headset->input_dev->name = pdev->name;
	headset->input_dev->open = rk_Hskey_open;
	headset->input_dev->close = rk_Hskey_close;
	headset->input_dev->dev.parent = &pdev->dev;
	//input_dev->phys = KEY_PHYS_NAME;
	headset->input_dev->id.vendor = 0x0001;
	headset->input_dev->id.product = 0x0001;
	headset->input_dev->id.version = 0x0100;
	// Register the input device 
	ret = input_register_device(headset->input_dev);
	if (ret) {
		dev_err(&pdev->dev, "failed to register input device\n");
		goto failed_free_dev;
	}

	input_set_capability(headset->input_dev, EV_KEY, pdata->hook_key_code);
//------------------------------------------------------------------
	if (pdata->Headset_gpio) {
		ret = pdata->headset_io_init(pdata->Headset_gpio, pdata->headset_gpio_info.iomux_name, pdata->headset_gpio_info.iomux_mode);
		if (ret) 
			goto failed_free;

		headset->irq[HEADSET] = gpio_to_irq(pdata->Headset_gpio);

		if(pdata->headset_in_type == HEADSET_IN_HIGH)
			headset->irq_type[HEADSET] = IRQF_TRIGGER_HIGH|IRQF_ONESHOT;
		else
			headset->irq_type[HEADSET] = IRQF_TRIGGER_LOW|IRQF_ONESHOT;
		ret = request_threaded_irq(headset->irq[HEADSET], NULL,headset_interrupt, headset->irq_type[HEADSET]|IRQF_NO_SUSPEND, "headset_input", NULL);
		if (ret) 
			goto failed_free;
		enable_irq_wake(headset->irq[HEADSET]);
	}
	else
		goto failed_free;
//------------------------------------------------------------------
	if(pdata->Hook_adc_chn>=0 && 3>=pdata->Hook_adc_chn)
	{
		headset->client = adc_register(pdata->Hook_adc_chn, hook_adc_callback, (void *)headset);
		if(!headset->client) {
			printk("hook adc register error\n");
			ret = -EINVAL;
			goto failed_free;
		}
		setup_timer(&headset->hook_timer,hook_timer_callback, (unsigned long)headset);	
		printk("headset adc default value = %d\n",adc_sync_read(headset->client));
	}
	
#ifdef CONFIG_HAS_EARLYSUSPEND
	hs_early_suspend.suspend = NULL;
	hs_early_suspend.resume = headset_early_resume;
	hs_early_suspend.level = ~0x0;
	register_early_suspend(&hs_early_suspend);
#endif

	return 0;	
	
failed_free_dev:
	platform_set_drvdata(pdev, NULL);
	input_free_device(headset->input_dev);
failed_free:
	dev_err(&pdev->dev, "failed to headset probe\n");
	kfree(headset);
	return ret;
}
示例#14
0
int stm32_adc_setup(void)
{
  static bool initialized = false;
  FAR struct adc_dev_s *adc;
  int ret;
  int i;

  /* Check if we have already initialized */

  if (!initialized)
    {
      /* DEV1 */
      /* Configure the pins as analog inputs for the selected channels */

      for (i = 0; i < DEV1_NCHANNELS; i++)
        {
          stm32_configgpio(g_pinlist1[i]);
        }

      /* Call stm32_adcinitialize() to get an instance of the ADC interface */

      adc = stm32_adcinitialize(DEV1_PORT, g_chanlist1, DEV1_NCHANNELS);
      if (adc == NULL)
        {
          aerr("ERROR: Failed to get ADC interface 1\n");
          return -ENODEV;
        }

      /* Register the ADC driver at "/dev/adc0" */

      ret = adc_register("/dev/adc0", adc);
      if (ret < 0)
        {
          aerr("ERROR: adc_register /dev/adc0 failed: %d\n", ret);
          return ret;
        }

#ifdef DEV2_PORT

      /* DEV2 */
      /* Configure the pins as analog inputs for the selected channels */

      for (i = 0; i < DEV2_NCHANNELS; i++)
        {
          stm32_configgpio(g_pinlist2[i]);
        }

      /* Call stm32_adcinitialize() to get an instance of the ADC interface */

      adc = stm32_adcinitialize(DEV2_PORT, g_chanlist2, DEV2_NCHANNELS);
      if (adc == NULL)
        {
          aerr("ERROR: Failed to get ADC interface 2\n");
          return -ENODEV;
        }

      /* Register the ADC driver at "/dev/adc1" */

      ret = adc_register("/dev/adc1", adc);
      if (ret < 0)
        {
          aerr("ERROR: adc_register /dev/adc1 failed: %d\n", ret);
          return ret;
        }
#endif

      initialized = true;

    }

  return OK;
}
示例#15
0
文件: tiva_adclow.c 项目: dagar/NuttX
int tiva_adc_initialize(const char *devpath, struct tiva_adc_cfg_s *cfg,
                                      uint32_t clock, uint8_t sample_rate)
{
  struct tiva_adc_s *adc;
  int ret = 0;

  ainfo("initializing...\n");

  /* Initialize the private ADC device data structure */

  adc = tiva_adc_struct_init(cfg);
  if (adc == NULL)
    {
      aerr("ERROR: Invalid ADC device number: expected=%d actual=%d\n",
           0, cfg->adc);
      return -ENODEV;
    }

  /* Turn on peripheral */

  if (tiva_adc_enable(adc->devno, true) < 0)
    {
      aerr("ERROR: failure to power ADC peripheral (devno=%d)\n",
           cfg->adc);
      return ret;
    }

  /* Perform actual initialization */

  tiva_adc_one_time_init(clock, sample_rate);
  tiva_adc_configure(cfg);
  tiva_adc_irqinitialize(cfg);

  /* Now we are initialized */

  adc->ena = true;
  adc->cb  = NULL;

#ifdef CONFIG_DEBUG_ANALOG
  tiva_adc_runtimeobj_vals();
#endif

  /* Ensure our lower half is valid */

  if (adc->dev == NULL)
    {
      aerr("ERROR: Failed to get interface %s\n", devpath);
      return -ENODEV;
    }

  ainfo("adc_dev_s=0x%08x\n", adc->dev);

  /* Register the ADC driver */

  ainfo("Register the ADC driver at %s\n", devpath);

  ret = adc_register(devpath, adc->dev);
  if (ret < 0)
    {
      aerr("ERROR: Failed to register %s to character driver: %d\n",
           devpath, ret);
      return ret;
    }

  return OK;
}
示例#16
0
static int rk2918_battery_probe(struct platform_device *pdev)
{
	int ret;
	struct adc_client *client;
	struct rk2918_battery_data *data;
	struct rk2918_battery_platform_data *pdata = pdev->dev.platform_data;
	int irq_flag;
	int i = 0;
	
	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (data == NULL) {
		ret = -ENOMEM;
		goto err_data_alloc_failed;
	}
	gBatteryData = data;

    //clear io
    data->dc_det_pin     = INVALID_GPIO;
    data->batt_low_pin   = INVALID_GPIO;
    data->charge_set_pin = INVALID_GPIO;
	data->charge_ok_pin  = INVALID_GPIO;
	
	if (pdata && pdata->io_init) {
		ret = pdata->io_init();
		if (ret) 
			goto err_free_gpio1;		
	}
	
	//dc det
	if (pdata->dc_det_pin != INVALID_GPIO)
	{
	#ifndef DC_DET_WITH_USB_INT
    	ret = gpio_request(pdata->dc_det_pin, NULL);
    	if (ret) {
    		printk("failed to request dc_det gpio\n");
    		goto err_free_gpio1;
    	}
	#endif
		if(pdata->dc_det_level)
    		gpio_pull_updown(pdata->dc_det_pin, 0);//important
    	else
			gpio_pull_updown(pdata->dc_det_pin, GPIOPullUp);//important
    	ret = gpio_direction_input(pdata->dc_det_pin);
    	if (ret) {
    		printk("failed to set gpio dc_det input\n");
    		goto err_free_gpio1;
    	}
    	data->dc_det_pin   = pdata->dc_det_pin;
    	data->dc_det_level = pdata->dc_det_level;
    }

	if (pdata->charge_cur_ctl != INVALID_GPIO) {

		ret = gpio_request(pdata->charge_cur_ctl, "DC_CURRENT_CONTROL");
		if (ret < 0) {
    		printk("failed to request charge current control gpio\n");
    		goto err_free_gpio2;
		}

		ret = gpio_direction_output(pdata->charge_cur_ctl, !pdata->charge_cur_ctl_level);
		if (ret < 0) {
			printk("rk29_battery: failed to set charge current control gpio\n");
    		goto err_free_gpio2;
		}
		gpio_pull_updown(pdata->charge_cur_ctl, !pdata->charge_cur_ctl_level);
		gpio_set_value(pdata->charge_cur_ctl, !pdata->charge_cur_ctl_level);
		data->charge_cur_ctl = pdata->charge_cur_ctl;
		data->charge_cur_ctl_level = pdata->charge_cur_ctl_level;
	}
	
	//charge set for usb charge
	if (pdata->charge_set_pin != INVALID_GPIO)
	{
    	ret = gpio_request(pdata->charge_set_pin, NULL);
    	if (ret) {
    		printk("failed to request dc_det gpio\n");
    		goto err_free_gpio2;
    	}
    	data->charge_set_pin = pdata->charge_set_pin;
    	data->charge_set_level = pdata->charge_set_level;
    	gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
    }
	
	//charge_ok
	if (pdata->charge_ok_pin != INVALID_GPIO)
	{
        ret = gpio_request(pdata->charge_ok_pin, NULL);
    	if (ret) {
    		printk("failed to request charge_ok gpio\n");
    		goto err_free_gpio3;
    	}
	
    	gpio_pull_updown(pdata->charge_ok_pin, GPIOPullUp);//important
    	ret = gpio_direction_input(pdata->charge_ok_pin);
    	if (ret) {
    		printk("failed to set gpio charge_ok input\n");
    		goto err_free_gpio3;
    	}
    	data->charge_ok_pin   = pdata->charge_ok_pin;
    	data->charge_ok_level = pdata->charge_ok_level;
    }
    
	client = adc_register(0, rk2918_battery_callback, NULL);
    if(!client)
		goto err_adc_register_failed;
    
	memset(gBatVoltageSamples, 0, sizeof(gBatVoltageSamples));
	spin_lock_init(&data->lock);
    data->adc_val = adc_sync_read(client);
	data->client = client;
    data->battery.properties = rk2918_battery_props;
	data->battery.num_properties = ARRAY_SIZE(rk2918_battery_props);
	data->battery.get_property = rk2918_battery_get_property;
	data->battery.name = "battery";
	data->battery.type = POWER_SUPPLY_TYPE_BATTERY;
	data->adc_bat_divider = 414;
	data->bat_max = BATT_MAX_VOL_VALUE;
	data->bat_min = BATT_ZERO_VOL_VALUE;
	DBG("bat_min = %d\n",data->bat_min);
	
#ifdef RK29_USB_CHARGE_SUPPORT
	data->usb.properties = rk2918_usb_props;
	data->usb.num_properties = ARRAY_SIZE(rk2918_usb_props);
	data->usb.get_property = rk2918_usb_get_property;
	data->usb.name = "usb";
	data->usb.type = POWER_SUPPLY_TYPE_USB;
#endif

	data->ac.properties = rk2918_ac_props;
	data->ac.num_properties = ARRAY_SIZE(rk2918_ac_props);
	data->ac.get_property = rk2918_ac_get_property;
	data->ac.name = "ac";
	data->ac.type = POWER_SUPPLY_TYPE_MAINS;	
	
	rk2918_low_battery_check();
    
	ret = power_supply_register(&pdev->dev, &data->ac);
	if (ret)
	{
		printk(KERN_INFO "fail to ac power_supply_register\n");
		goto err_ac_failed;
	}
#if 0
	ret = power_supply_register(&pdev->dev, &data->usb);
	if (ret)
	{
		printk(KERN_INFO "fail to usb power_supply_register\n");
		goto err_usb_failed;
	}
#endif
	ret = power_supply_register(&pdev->dev, &data->battery);
	if (ret)
	{
		printk(KERN_INFO "fail to battery power_supply_register\n");
		goto err_battery_failed;
	}
	platform_set_drvdata(pdev, data);
	
	INIT_WORK(&data->timer_work, rk2918_battery_timer_work);
	
//    irq_flag = (pdata->charge_ok_level) ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
//	ret = request_irq(gpio_to_irq(pdata->charge_ok_pin), rk2918_battery_interrupt, irq_flag, "rk2918_battery", data);
//	if (ret) {
//		printk("failed to request irq\n");
//		goto err_irq_failed;
//	}
#ifndef DC_DET_WITH_USB_INT
    if (pdata->dc_det_pin != INVALID_GPIO)
    {
        irq_flag = (!gpio_get_value (pdata->dc_det_pin)) ? IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
    	ret = request_irq(gpio_to_irq(pdata->dc_det_pin), rk2918_dc_wakeup, irq_flag, "rk2918_battery", data);
    	if (ret) {
    		printk("failed to request dc det irq\n");
    		goto err_dcirq_failed;
    	}
    	data->dc_det_irq = gpio_to_irq(pdata->dc_det_pin);
    	//data->wq = create_rt_workqueue("rk2918_battery");
    	data->wq = create_workqueue("rk2918_battery");
    	INIT_DELAYED_WORK(&data->work, rk2918_battery_work);
    	
    	enable_irq_wake(gpio_to_irq(pdata->dc_det_pin));
    }
#endif

	setup_timer(&data->timer, rk2918_batscan_timer, (unsigned long)data);
	//data->timer.expires  = jiffies + 2000;
	//add_timer(&data->timer);
   
    rk29_battery_dbg_class = class_create(THIS_MODULE, "rk29_battery");
	battery_dev = device_create(rk29_battery_dbg_class, NULL, MKDEV(0, 1), NULL, "battery");
	ret = device_create_file(battery_dev, &dev_attr_rk29_battery_dbg);
	if (ret)
	{
		printk("create file sys failed!!! \n");
		//goto err_dcirq_failed;
	}
	for(i = 0; i<10; i++)
	{
		ret = device_create_file(&pdev->dev, &dev_attr_startget);
		if (ret)
		{
			printk("make a mistake in creating devices  attr file, failed times: %d\n\n ", i+1);
			continue;
		}
		break;
	}
	printk(KERN_INFO "rk2918_battery: driver initialized\n");
	
	return 0;
	
err_dcirq_failed:
    free_irq(gpio_to_irq(pdata->dc_det_pin), data);
    
err_irq_failed:
	free_irq(gpio_to_irq(pdata->charge_ok_pin), data);
    
err_battery_failed:
//	power_supply_unregister(&data->usb);
//err_usb_failed:
err_ac_failed:
	power_supply_unregister(&data->ac);
	
err_adc_register_failed:
err_free_gpio3:
	gpio_free(pdata->charge_ok_pin);
err_free_gpio2:
	gpio_free(pdata->charge_cur_ctl);
err_free_gpio1:
    gpio_free(pdata->dc_det_pin);
    
err_data_alloc_failed:
	kfree(data);

    printk("rk2918_battery: error!\n");
    
	return ret;
}
示例#17
0
static int rk30_adc_probe(struct platform_device *pdev)
{
        struct adc_platform_data *pdata = pdev->dev.platform_data;
	struct adc_host *adc = NULL;
	struct rk30_adc_device *dev;
	struct resource *res;
	int ret = 0, i, v;

        if(!pdata)
                return -EINVAL;

	adc = adc_alloc_host(&pdev->dev, sizeof(struct rk30_adc_device), SARADC_CHN_MASK);
	if (!adc)
		return -ENOMEM;
	adc->ops = &rk30_adc_ops;
        adc->pdata = pdata;
	dev = adc_priv(adc);
	dev->adc = adc;
	dev->irq = platform_get_irq(pdev, 0);
	if (dev->irq <= 0) {
		dev_err(&pdev->dev, "failed to get adc irq\n");
		ret = -ENOENT;
		goto err_alloc;
	}

	ret = request_threaded_irq(dev->irq, NULL, rk30_adc_irq, IRQF_ONESHOT, pdev->name, dev);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to attach adc irq\n");
		goto err_alloc;
	}

	dev->pclk = clk_get(&pdev->dev, "pclk_saradc");
	if (IS_ERR(dev->pclk)) {
		dev_err(&pdev->dev, "failed to get adc pclk\n");
		ret = PTR_ERR(dev->pclk);
		goto err_irq;
	}
	clk_enable(dev->pclk);

	dev->clk = clk_get(&pdev->dev, "saradc");
	if (IS_ERR(dev->clk)) {
		dev_err(&pdev->dev, "failed to get adc clock\n");
		ret = PTR_ERR(dev->clk);
		goto err_pclk;
	}

	ret = clk_set_rate(dev->clk, ADC_CLK_RATE * 1000 * 1000);
	if(ret < 0) {
		dev_err(&pdev->dev, "failed to set adc clk\n");
		goto err_clk2;
	}
	clk_enable(dev->clk);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "cannot find IO resource\n");
		ret = -ENOENT;
		goto err_clk;
	}
	dev->ioarea = request_mem_region(res->start, (res->end - res->start) + 1, 
									pdev->name);
	if(dev->ioarea == NULL) {
		dev_err(&pdev->dev, "cannot request IO\n");
		ret = -ENXIO;
		goto err_clk;
	}
	dev->regs = ioremap(res->start, (res->end - res->start) + 1);
	if (!dev->regs) {
		dev_err(&pdev->dev, "cannot map IO\n");
		ret = -ENXIO;
		goto err_ioarea;
	}
        g_adc = adc;
	platform_set_drvdata(pdev, dev);

        if(adc->pdata->base_chn > 0){
                adc->base_client = adc_register(adc->pdata->base_chn, NULL, NULL);
                if(!adc->base_client){
		        dev_err(&pdev->dev, "adc_register(base_chn: %d) failed\n", adc->pdata->base_chn);
                        ret = -ENOMEM;
                        goto err_adc_register;
                }
                for(i = 0; i < SAMPLE_COUNT; i++){
                        v = adc_sync_read(adc->base_client);
                        if(v < 0){
		                dev_err(&pdev->dev, "adc_register(base_chn: %d) failed\n", adc->pdata->base_chn);
                                ret = v;
                                goto err_adc_sync_read;
                        }else if(v < MIN_SAMPLE_VALUE){
		                dev_info(&pdev->dev, "chn[%d]: adc value(%d) is invalide\n", adc->pdata->base_chn, v);
                                adc_unregister(adc->base_client);
                                adc->base_client = NULL;
                                break;
                        }
                        adc_dbg(&pdev->dev, "read ref_adc: %d\n", v);
                        mdelay(1);
                }
        }
	dev_info(&pdev->dev, "rk30 adc: driver initialized\n");
	return 0;
err_adc_sync_read:
        adc_unregister(adc->base_client);
        adc->base_client = NULL;
err_adc_register:
	iounmap(dev->regs);
err_ioarea:
	release_resource(dev->ioarea);
	kfree(dev->ioarea);

err_clk:
	clk_disable(dev->clk);

err_pclk:
	clk_disable(dev->pclk);
	clk_put(dev->pclk);

err_clk2:
	clk_put(dev->clk);

err_irq:
	free_irq(dev->irq, dev);

err_alloc:
        adc_free_host(dev->adc);
	return ret;
}