void vibtonz_pwm(int nForce) { static int prev_duty=0; t_vib_desc *vib_iter =&vib_desc; int pwm_period=0, pwm_duty = 0; printk("%s : %d \n", __func__, nForce); if( vib_iter->initialized == 0) return; pwm_period = vib_iter->pwm_period; pwm_duty = pwm_period/2 + ((pwm_period/2 - 2) *nForce) /127; if(pwm_duty > vib_iter->pwm_duty) { pwm_duty = vib_iter->pwm_duty; } else if(pwm_period - pwm_duty > vib_iter->pwm_duty) { pwm_duty = pwm_period - vib_iter->pwm_duty; } pwm_set_period_ns(vib_iter->pwm,vib_iter->pwm_period); pwm_set_polarity(vib_iter->pwm, vib_iter->pwm_polarity); pwm_set_duty_ns(vib_iter->pwm, pwm_duty); }
static PyObject *py_set_polarity(PyObject *self, PyObject *args, PyObject *kwargs){ char key[8]; char *channel; int polarity = 0; static char *kwlist[] = {"channel", "polarity", NULL}; if(!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &channel, &polarity)) return NULL; if (polarity != 0 && polarity != 1) { PyErr_SetString(PyExc_ValueError, "polarity must be either 0 or 1"); return NULL; } if (!get_pwm_key(channel, key)) { PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); return NULL; } if (pwm_set_polarity(key, polarity) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } Py_RETURN_NONE; }
static int __devinit pwm_ir_tx_probe(struct pwm_ir_dev *dev) { struct pwm_ir_data *data = dev->pdev->dev.platform_data; int rc = 0; if (data->reg_id) { dev->reg = regulator_get(&dev->pdev->dev, data->reg_id); if (IS_ERR(dev->reg)) { dev_err(&dev->pdev->dev, "failed to regulator_get(%s)\n", data->reg_id); return PTR_ERR(dev->reg); } } dev->pwm = pwm_request(data->pwm_id, PWM_IR_NAME); if (IS_ERR(dev->pwm)) { dev_err(&dev->pdev->dev, "failed to pwm_request(%d)\n", data->pwm_id); rc = PTR_ERR(dev->pwm); goto err_regulator_put; } if (data->low_active) { #if 0 /* need the latest kernel */ rc = pwm_set_polarity(dev->pwm, PWM_POLARITY_INVERSED); #else rc = -ENOSYS; #endif if (rc != 0) { dev_err(&dev->pdev->dev, "failed to change polarity\n"); goto err_pwm_free; } } rc = pwm_ir_tx_config(dev, 38000, 50); if (rc != 0) { dev_err(&dev->pdev->dev, "failed to change carrier and duty\n"); goto err_pwm_free; } dev->rdev->tx_ir = pwm_ir_tx_transmit; dev->rdev->s_tx_carrier = pwm_ir_tx_carrier; dev->rdev->s_tx_duty_cycle = pwm_ir_tx_duty_cycle; return rc; err_pwm_free: pwm_free(dev->pwm); err_regulator_put: if (dev->reg) regulator_put(dev->reg); return rc; }
void servo_init() { int i; for(i=0;i<sizeof(servo_pin)/sizeof(servo_pin[0]);i++) { pwm_stop(servo_pin[i][0], servo_pin[i][1]); pwm_set_period(servo_pin[i][0], servo_pin[i][1], SERVO_STD_PERIOD); pwm_set_polarity(servo_pin[i][0], servo_pin[i][1], 1); pwm_set_duty_cycle(servo_pin[i][0], servo_pin[i][1], SERVO_ANGLE_TO_DUTY(0)); /* keep in the middle */ } start_servo(); }
int OSAL_Pwm_Set_Polarity(__hdle p_handler, int polarity) { int ret = 0; struct pwm_device *pwm_dev; pwm_dev = (struct pwm_device *)p_handler; if(NULL == pwm_dev || IS_ERR(pwm_dev)) { __wrn("OSAL_Pwm_Set_Polarity, handle is NULL!\n"); ret = -1; } else { ret = pwm_set_polarity(pwm_dev, polarity); __inf("OSAL_Pwm_Set_Polarity pwm %d, active %s\n", pwm_dev->pwm, (polarity==0)? "high":"low"); } return ret; }
static ssize_t pwm_polarity_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { unsigned long polarity; struct pwm_device *p = dev_get_drvdata(dev); int ret; if (!kstrtoul(buf, 10, &polarity)) { ret = pwm_set_polarity(p, polarity); if (ret < 0) return ret; } return len; }
static ssize_t pwm_test_store_polarity(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int rc; struct pwm_test *pwm_test = dev_get_drvdata(dev); rc = kstrtoul(buf, 0, &pwm_test->polarity_s); if (rc) return rc; rc = pwm_set_polarity(pwm_test->pwm, pwm_test->polarity_s); if (rc) { pr_err("operation failed %d\n", rc); return rc; } pwm_test->polarity = pwm_test->polarity_s; return count; }
static ssize_t pwm_test_store_polarity(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int rc; struct pwm_test *pwm_test = dev_get_drvdata(dev); int val; enum pwm_polarity polarity; rc = kstrtoint(buf, 0, &val); if (rc) return rc; /* only zero and one allowed */ if (val != 0 && val != 1) return -EINVAL; polarity = val ? PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL; /* same? don't do anything */ if (polarity == pwm_test->polarity) return count; /* polarity can only change when we stop the pwm */ if (pwm_test->run) pwm_disable(pwm_test->pwm); rc = pwm_set_polarity(pwm_test->pwm, polarity); if (rc) { dev_err(dev, "pwm_set_polarity failed\n"); if (pwm_test->run) pwm_enable(pwm_test->pwm); return rc; } if (pwm_test->run) pwm_enable(pwm_test->pwm); pwm_test->polarity = polarity; return count; }
static int pwm_backlight_probe(struct platform_device *pdev) { struct backlight_properties props; struct platform_pwm_backlight_data *data = pdev->dev.platform_data; struct backlight_device *bl; struct pwm_bl_data *pb; int ret; if (!data) { dev_err(&pdev->dev, "failed to find platform data\n"); return -EINVAL; } if (data->init) { ret = data->init(&pdev->dev); if (ret < 0) return ret; } pb = kzalloc(sizeof(*pb), GFP_KERNEL); if (!pb) { dev_err(&pdev->dev, "no memory for state\n"); ret = -ENOMEM; goto err_alloc; } pb->period = data->pwm_period_ns; pb->notify = data->notify; pb->check_fb = data->check_fb; pb->lth_brightness = data->lth_brightness * (data->pwm_period_ns / data->max_brightness); pb->dev = &pdev->dev; pb->pwm = pwm_request(data->pwm_name, "backlight"); if (IS_ERR(pb->pwm)) { dev_err(&pdev->dev, "unable to request PWM for backlight\n"); ret = PTR_ERR(pb->pwm); goto err_pwm; } else dev_dbg(&pdev->dev, "got pwm for backlight\n"); memset(&props, 0, sizeof(struct backlight_properties)); props.type = BACKLIGHT_RAW; props.max_brightness = data->max_brightness; bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, &pwm_backlight_ops, &props); if (IS_ERR(bl)) { dev_err(&pdev->dev, "failed to register backlight\n"); ret = PTR_ERR(bl); goto err_bl; } bl->props.brightness = data->dft_brightness; pwm_set_polarity(pb->pwm, data->polarity); backlight_update_status(bl); platform_set_drvdata(pdev, bl); return 0; err_bl: pwm_release(pb->pwm); err_pwm: kfree(pb); err_alloc: if (data->exit) data->exit(&pdev->dev); return ret; }
int pwm_start(const char *key, float duty, float freq, int polarity) { char fragment[18]; char pwm_test_fragment[20]; char pwm_test_path[45]; char period_path[50]; char duty_path[50]; char polarity_path[55]; int period_fd, duty_fd, polarity_fd; struct pwm_exp *new_pwm, *pwm; if(!BBB_G(pwm_initialized)) { initialize_pwm(); } snprintf(fragment, sizeof(fragment), "bone_pwm_%s", key); if (!load_device_tree(fragment)) { //error enabling pin for pwm return -1; } //creates the fragment in order to build the pwm_test_filename, such as "pwm_test_P9_13" snprintf(pwm_test_fragment, sizeof(pwm_test_fragment), "pwm_test_%s", key); //finds and builds the pwm_test_path, as it can be variable... build_path(BBB_G(ocp_dir), pwm_test_fragment, pwm_test_path, sizeof(pwm_test_path)); //create the path for the period and duty snprintf(period_path, sizeof(period_path), "%s/period", pwm_test_path); snprintf(duty_path, sizeof(duty_path), "%s/duty", pwm_test_path); snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_test_path); //add period and duty fd to pwm list if ((period_fd = open(period_path, O_RDWR)) < 0) return -1; if ((duty_fd = open(duty_path, O_RDWR)) < 0) { //error, close already opened period_fd. close(period_fd); return -1; } if ((polarity_fd = open(polarity_path, O_RDWR)) < 0) { //error, close already opened period_fd and duty_fd. close(period_fd); close(duty_fd); return -1; } // add to list new_pwm = malloc(sizeof(struct pwm_exp)); if (new_pwm == 0) { return -1; // out of memory } strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */ new_pwm->period_fd = period_fd; new_pwm->duty_fd = duty_fd; new_pwm->polarity_fd = polarity_fd; new_pwm->next = NULL; if (exported_pwms == NULL) { // create new list exported_pwms = new_pwm; } else { // add to end of existing list pwm = exported_pwms; while (pwm->next != NULL) pwm = pwm->next; pwm->next = new_pwm; } pwm_set_frequency(key, freq); pwm_set_polarity(key, polarity); pwm_set_duty_cycle(key, duty); return 1; }
static int ss_brcm_haptic_probe(struct platform_device *pdev) { struct platform_isa1000_vibrator_data *pdata = pdev->dev.platform_data; t_vib_desc *vib_iter; int ret=0; printk("ss_brcm_haptic_probe \n"); /* vib_iter=kzalloc(sizeof(t_vib_desc), GFP_KERNEL); if(vib_iter == NULL) { pr_err("%s : memory allocation failure \n", __func__); return -ENOMEM; } */ vib_iter=&vib_desc; vib_iter->vib_vcc = (const char *)pdata->regulator_id; printk(KERN_INFO "%s: Vibrator vcc=%s \n", __func__, vib_iter->vib_vcc); //vib_iter->vib_regulator=regulator_get(NULL, VIB_VCC); vib_iter->vib_regulator=regulator_get(NULL, vib_iter->vib_vcc); if(IS_ERR(vib_iter->vib_regulator)) { printk(KERN_INFO "%s: failed to get regulator \n", __func__); return -ENODEV; } regulator_enable(vib_iter->vib_regulator); vib_iter->gpio_en = pdata->gpio_en; vib_iter->pwm = pwm_request(pdata->pwm_name, "vibrator"); if (IS_ERR(vib_iter->pwm)) { pr_err("[VIB] Failed to request pwm.\n"); return -EFAULT; } vib_iter->pwm_duty = pdata->pwm_duty; vib_iter->pwm_period = pdata->pwm_period_ns; vib_iter->pwm_polarity = pdata->polarity; pwm_set_polarity(vib_iter->pwm , vib_iter->pwm_polarity); vib_iter->timed_dev.name="vibrator"; vib_iter->timed_dev.enable=vibrator_enable_set_timeout; vib_iter->timed_dev.get_time=vibrator_get_remaining_time; ret = timed_output_dev_register(&vib_iter->timed_dev); if(ret < 0) { printk(KERN_ERR "Vibrator: timed_output dev registration failure\n"); timed_output_dev_unregister(&vib_iter->timed_dev); } init_timer(&vib_iter->vib_timer); vib_iter->vib_timer.function = on_vibrate_timer_expired; vib_iter->vib_timer.data = (unsigned long)vib_iter; platform_set_drvdata(pdev, vib_iter); INIT_WORK(&vib_iter->off_work, vibrator_off_work_func); vib_iter->initialized = 1; printk("%s : ss vibrator probe\n", __func__); return 0; }
void Pwm::set_polarity(Polarity polarity) { (CheckError)pwm_set_polarity(key_.c_str(), (int)polarity); }
int pwm_start(const char *pinName, int exportNumber, char *pwmDir, float duty, float freq, int polarity, PwmChannel_t *pwm) { char pwm_control_path[PATHLEN]; char pwm_path[PATHLEN]; //pwm channel control folder char export[2]; int fd; strncpy(pwm->key, pinName, KEYLEN); if (set_pinmux(pinName, "pwm") < 0) { return -1; } //export pwm channel if ((fd = open("/sys/class/pwm/export", O_WRONLY)) < 0) { LogError("pwm open(/sys/class/pwm/export) fail (%s)\n", strerror(errno)); return -1; } snprintf(export, 2, "%i", exportNumber); write(fd,export,1); close(fd); snprintf(pwm_path, sizeof(pwm_path), "/sys/class/pwm/%s", pwmDir); //create the path for period control snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/period_ns", pwm_path); if ((pwm->period_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/duty_ns", pwm_path); if ((pwm->duty_fd = open(pwm_control_path, O_WRONLY)) < 0) { //error, close already opened period_fd. LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); close(pwm->period_fd); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/polarity", pwm_path); if ((pwm->polarity_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); //error, close already opened period_fd and duty_fd. close(pwm->period_fd); close(pwm->duty_fd); return -1; } snprintf(pwm_control_path, sizeof(pwm_control_path), "%s/run", pwm_path); if ((pwm->run_fd = open(pwm_control_path, O_WRONLY)) < 0) { LogError("pwm open(%s) fail (%s)\n", pwm_control_path, strerror(errno)); //error, close already opened period_fd and duty_fd. close(pwm->polarity_fd); close(pwm->period_fd); close(pwm->duty_fd); return -1; } pwm_set_frequency(pwm, freq); pwm_set_polarity(pwm, polarity); pwm_set_duty_cycle(pwm, duty); pwm_set_run(pwm, 1); return 0; }
static int pwm_backlight_probe(struct platform_device *pdev) { struct backlight_properties props; struct platform_pwm_backlight_data *data = NULL; struct backlight_device *bl; struct pwm_bl_data *pb; const char *pwm_request_label = NULL; int ret; int bl_delay_on = 0; printk("[BACKLIGHT] %s : %d\n", __func__, __LINE__); if (pdev->dev.platform_data) data = pdev->dev.platform_data; else if (pdev->dev.of_node) { u32 val; data = kzalloc(sizeof(struct platform_pwm_backlight_data), GFP_KERNEL); if (!data) return -ENOMEM; if (of_property_read_u32(pdev->dev.of_node, "pwm-id", &val)) { ret = -EINVAL; goto err_read; } data->pwm_id = val; if (of_property_read_u32(pdev->dev.of_node, "max-brightness", &val)) { ret = -EINVAL; goto err_read; } data->max_brightness = val; if (of_property_read_u32(pdev->dev.of_node, "dft-brightness", &val)) { ret = -EINVAL; goto err_read; } data->dft_brightness = val; if (of_property_read_u32(pdev->dev.of_node, "polarity", &val)) { ret = -EINVAL; goto err_read; } data->polarity = val; if (of_property_read_u32(pdev->dev.of_node, "pwm-period-ns", &val)) { ret = -EINVAL; goto err_read; } data->pwm_period_ns = val; if (of_property_read_string(pdev->dev.of_node, "pwm-request-label", &pwm_request_label)) { ret = -EINVAL; goto err_read; } if (of_property_read_u32(pdev->dev.of_node, "bl-on-delay", &val)) { bl_delay_on = 0; } else bl_delay_on = val; if (of_property_read_u32(pdev->dev.of_node, "pwm_pin_name", &val)) { pwm_pin = -1; } else pwm_pin = val; if (of_property_read_u32(pdev->dev.of_node, "pwm_pin_reboot_func", &val)) { pwm_pin_reboot_func = -1; } else pwm_pin_reboot_func = val; pdev->dev.platform_data = data; } if (!data) { dev_err(&pdev->dev, "failed to find platform data\n"); return -EINVAL; } if (data->init) { ret = data->init(&pdev->dev); if (ret < 0) return ret; } pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); if (!pb) { dev_err(&pdev->dev, "no memory for state\n"); ret = -ENOMEM; goto err_alloc; } pb->period = data->pwm_period_ns; pb->notify = data->notify; pb->notify_after = data->notify_after; pb->check_fb = data->check_fb; pb->lth_brightness = data->lth_brightness * (data->pwm_period_ns / data->max_brightness); pb->dev = &pdev->dev; if (pdev->dev.of_node) pb->pwm = pwm_request(data->pwm_id, pwm_request_label); else pb->pwm = pwm_request(data->pwm_id, "backlight"); if (IS_ERR(pb->pwm)) { dev_err(&pdev->dev, "unable to request PWM for backlight\n"); ret = PTR_ERR(pb->pwm); goto err_alloc; } else dev_dbg(&pdev->dev, "got pwm for backlight\n"); memset(&props, 0, sizeof(struct backlight_properties)); props.type = BACKLIGHT_RAW; props.max_brightness = data->max_brightness; //bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, // &pwm_backlight_ops, &props); bl = backlight_device_register("panel", &pdev->dev, pb, &pwm_backlight_ops, &props); if (IS_ERR(bl)) { dev_err(&pdev->dev, "failed to register backlight\n"); ret = PTR_ERR(bl); goto err_bl; } bl->props.brightness = data->dft_brightness; pwm_set_polarity(pb->pwm, data->polarity); pr_info("pwm_backlight_probe bl-delay-on %d\r\n", bl_delay_on); pr_info("pwm_backlight_probe pwm_pin %d\r\n", pwm_pin); pr_info("pwm_backlight_probe pwm_pin_reboot_func %d\r\n", pwm_pin_reboot_func); if (bl_delay_on == 0) backlight_update_status(bl); else { INIT_DELAYED_WORK(&(pb->bl_delay_on_work), bl_delay_on_func); schedule_delayed_work(&(pb->bl_delay_on_work), msecs_to_jiffies(bl_delay_on)); } platform_set_drvdata(pdev, bl); #ifdef CONFIG_BACKLIGHT_USE_EARLYSUSPEND pb->bd_early_suspend.suspend = backlight_driver_early_suspend; pb->bd_early_suspend.resume = backlight_driver_late_resume; pb->bd_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN; register_early_suspend(&pb->bd_early_suspend); #endif printk("[BACKLIGHT] %s : %d\n", __func__, __LINE__); return 0; err_bl: pwm_free(pb->pwm); err_alloc: if (data->exit) data->exit(&pdev->dev); err_read: if (pdev->dev.of_node) kfree(data); return ret; }