Re: [PATCH v4 3/4] pwm: add LP3943 PWM driver

2013-12-04 Thread Thierry Reding
On Wed, Sep 25, 2013 at 01:24:38PM +0900, Milo Kim wrote:
> This is the other of the LP3943 MFD driver.
> LP3943 can be used as a PWM generator, up to 2 channels.
> 
> * Two PWM generators supported
> 
> * Supported PWM operations
>   request, free, config, enable and disable
> 
> * Pin assignment
>   A driver data, 'pin_used' is checked when a PWM is requested.
>   If the output pin is already assigned, then returns as failure.
>   If the pin is available, 'pin_used' is set.
>   When the PWM is not used anymore, then it is cleared.
>   It is defined as unsigned long type for atomic bit operation APIs,
>   but only LSB 16bits are used because LP3943 has 16 outputs.
> 
> Cc: Thierry Reding 
> Signed-off-by: Milo Kim 
> ---
> * Patch v4
>   No update, same as v3.
> 
> * Patch v3
>   Manual polarity not supported any more.
>   Add encapsulation functions for accessing internal data structure.
>   Use pwm_set_chip_data() and pwm_get_chip_data().
>   And other code fixes based on Thierry's feedback.
> 
>  drivers/pwm/Kconfig  |   10 ++
>  drivers/pwm/Makefile |1 +
>  drivers/pwm/pwm-lp3943.c |  314 
> ++
>  3 files changed, 325 insertions(+)
>  create mode 100644 drivers/pwm/pwm-lp3943.c

Acked-by: Thierry Reding 


pgpXwzRdyWFqa.pgp
Description: PGP signature


Re: [PATCH v4 3/4] pwm: add LP3943 PWM driver

2013-12-04 Thread Thierry Reding
On Wed, Sep 25, 2013 at 01:24:38PM +0900, Milo Kim wrote:
 This is the other of the LP3943 MFD driver.
 LP3943 can be used as a PWM generator, up to 2 channels.
 
 * Two PWM generators supported
 
 * Supported PWM operations
   request, free, config, enable and disable
 
 * Pin assignment
   A driver data, 'pin_used' is checked when a PWM is requested.
   If the output pin is already assigned, then returns as failure.
   If the pin is available, 'pin_used' is set.
   When the PWM is not used anymore, then it is cleared.
   It is defined as unsigned long type for atomic bit operation APIs,
   but only LSB 16bits are used because LP3943 has 16 outputs.
 
 Cc: Thierry Reding thierry.red...@gmail.com
 Signed-off-by: Milo Kim milo@ti.com
 ---
 * Patch v4
   No update, same as v3.
 
 * Patch v3
   Manual polarity not supported any more.
   Add encapsulation functions for accessing internal data structure.
   Use pwm_set_chip_data() and pwm_get_chip_data().
   And other code fixes based on Thierry's feedback.
 
  drivers/pwm/Kconfig  |   10 ++
  drivers/pwm/Makefile |1 +
  drivers/pwm/pwm-lp3943.c |  314 
 ++
  3 files changed, 325 insertions(+)
  create mode 100644 drivers/pwm/pwm-lp3943.c

Acked-by: Thierry Reding thierry.red...@gmail.com


pgpXwzRdyWFqa.pgp
Description: PGP signature


[PATCH v4 3/4] pwm: add LP3943 PWM driver

2013-09-24 Thread Milo Kim
This is the other of the LP3943 MFD driver.
LP3943 can be used as a PWM generator, up to 2 channels.

* Two PWM generators supported

* Supported PWM operations
  request, free, config, enable and disable

* Pin assignment
  A driver data, 'pin_used' is checked when a PWM is requested.
  If the output pin is already assigned, then returns as failure.
  If the pin is available, 'pin_used' is set.
  When the PWM is not used anymore, then it is cleared.
  It is defined as unsigned long type for atomic bit operation APIs,
  but only LSB 16bits are used because LP3943 has 16 outputs.

Cc: Thierry Reding 
Signed-off-by: Milo Kim 
---
* Patch v4
  No update, same as v3.

* Patch v3
  Manual polarity not supported any more.
  Add encapsulation functions for accessing internal data structure.
  Use pwm_set_chip_data() and pwm_get_chip_data().
  And other code fixes based on Thierry's feedback.

 drivers/pwm/Kconfig  |   10 ++
 drivers/pwm/Makefile |1 +
 drivers/pwm/pwm-lp3943.c |  314 ++
 3 files changed, 325 insertions(+)
 create mode 100644 drivers/pwm/pwm-lp3943.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 75840b5..9cf38bc 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -81,6 +81,16 @@ config PWM_JZ4740
  To compile this driver as a module, choose M here: the module
  will be called pwm-jz4740.
 
+config PWM_LP3943
+   tristate "TI/National Semiconductor LP3943 PWM support"
+   depends on MFD_LP3943
+   help
+ Generic PWM framework driver for LP3943 which supports two PWM
+ channels.
+
+ To compile this driver as a module, choose M here: the module
+ will be called pwm-lp3943.
+
 config PWM_LPC32XX
tristate "LPC32XX PWM support"
depends on ARCH_LPC32XX
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 77a8c18..db8e349 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o
 obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o
 obj-$(CONFIG_PWM_IMX)  += pwm-imx.o
 obj-$(CONFIG_PWM_JZ4740)   += pwm-jz4740.o
+obj-$(CONFIG_PWM_LP3943)   += pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC32XX)  += pwm-lpc32xx.o
 obj-$(CONFIG_PWM_MXS)  += pwm-mxs.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
diff --git a/drivers/pwm/pwm-lp3943.c b/drivers/pwm/pwm-lp3943.c
new file mode 100644
index 000..8a843a0
--- /dev/null
+++ b/drivers/pwm/pwm-lp3943.c
@@ -0,0 +1,314 @@
+/*
+ * TI/National Semiconductor LP3943 PWM driver
+ *
+ * Copyright 2013 Texas Instruments
+ *
+ * Author: Milo Kim 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define LP3943_MAX_DUTY255
+#define LP3943_MIN_PERIOD  6250
+#define LP3943_MAX_PERIOD  160
+
+struct lp3943_pwm {
+   struct pwm_chip chip;
+   struct lp3943 *lp3943;
+   struct lp3943_platform_data *pdata;
+};
+
+static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *_chip)
+{
+   return container_of(_chip, struct lp3943_pwm, chip);
+}
+
+static struct lp3943_pwm_map *
+lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
+{
+   struct lp3943_platform_data *pdata = lp3943_pwm->pdata;
+   struct lp3943 *lp3943 = lp3943_pwm->lp3943;
+   struct lp3943_pwm_map *pwm_map;
+   int i, offset;
+
+   pwm_map = kzalloc(sizeof(*pwm_map), GFP_KERNEL);
+   if (!pwm_map)
+   return ERR_PTR(-ENOMEM);
+
+   pwm_map->output = pdata->pwms[hwpwm]->output;
+   pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
+
+   for (i = 0; i < pwm_map->num_outputs; i++) {
+   offset = pwm_map->output[i];
+
+   /* Return an error if the pin is already assigned */
+   if (test_and_set_bit(offset, >pin_used))
+   return ERR_PTR(-EBUSY);
+   }
+
+   return pwm_map;
+}
+
+static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+   struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
+   struct lp3943_pwm_map *pwm_map;
+
+   pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
+   if (IS_ERR(pwm_map))
+   return PTR_ERR(pwm_map);
+
+   return pwm_set_chip_data(pwm, pwm_map);
+}
+
+static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
+   struct lp3943_pwm_map *pwm_map)
+{
+   struct lp3943 *lp3943 = lp3943_pwm->lp3943;
+   int i, offset;
+
+   for (i = 0; i < pwm_map->num_outputs; i++) {
+   offset = pwm_map->output[i];
+   clear_bit(offset, >pin_used);
+   }
+
+   kfree(pwm_map);
+}
+
+static void 

[PATCH v4 3/4] pwm: add LP3943 PWM driver

2013-09-24 Thread Milo Kim
This is the other of the LP3943 MFD driver.
LP3943 can be used as a PWM generator, up to 2 channels.

* Two PWM generators supported

* Supported PWM operations
  request, free, config, enable and disable

* Pin assignment
  A driver data, 'pin_used' is checked when a PWM is requested.
  If the output pin is already assigned, then returns as failure.
  If the pin is available, 'pin_used' is set.
  When the PWM is not used anymore, then it is cleared.
  It is defined as unsigned long type for atomic bit operation APIs,
  but only LSB 16bits are used because LP3943 has 16 outputs.

Cc: Thierry Reding thierry.red...@gmail.com
Signed-off-by: Milo Kim milo@ti.com
---
* Patch v4
  No update, same as v3.

* Patch v3
  Manual polarity not supported any more.
  Add encapsulation functions for accessing internal data structure.
  Use pwm_set_chip_data() and pwm_get_chip_data().
  And other code fixes based on Thierry's feedback.

 drivers/pwm/Kconfig  |   10 ++
 drivers/pwm/Makefile |1 +
 drivers/pwm/pwm-lp3943.c |  314 ++
 3 files changed, 325 insertions(+)
 create mode 100644 drivers/pwm/pwm-lp3943.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 75840b5..9cf38bc 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -81,6 +81,16 @@ config PWM_JZ4740
  To compile this driver as a module, choose M here: the module
  will be called pwm-jz4740.
 
+config PWM_LP3943
+   tristate TI/National Semiconductor LP3943 PWM support
+   depends on MFD_LP3943
+   help
+ Generic PWM framework driver for LP3943 which supports two PWM
+ channels.
+
+ To compile this driver as a module, choose M here: the module
+ will be called pwm-lp3943.
+
 config PWM_LPC32XX
tristate LPC32XX PWM support
depends on ARCH_LPC32XX
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 77a8c18..db8e349 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -5,6 +5,7 @@ obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o
 obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o
 obj-$(CONFIG_PWM_IMX)  += pwm-imx.o
 obj-$(CONFIG_PWM_JZ4740)   += pwm-jz4740.o
+obj-$(CONFIG_PWM_LP3943)   += pwm-lp3943.o
 obj-$(CONFIG_PWM_LPC32XX)  += pwm-lpc32xx.o
 obj-$(CONFIG_PWM_MXS)  += pwm-mxs.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
diff --git a/drivers/pwm/pwm-lp3943.c b/drivers/pwm/pwm-lp3943.c
new file mode 100644
index 000..8a843a0
--- /dev/null
+++ b/drivers/pwm/pwm-lp3943.c
@@ -0,0 +1,314 @@
+/*
+ * TI/National Semiconductor LP3943 PWM driver
+ *
+ * Copyright 2013 Texas Instruments
+ *
+ * Author: Milo Kim milo@ti.com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2.
+ */
+
+#include linux/err.h
+#include linux/i2c.h
+#include linux/mfd/lp3943.h
+#include linux/module.h
+#include linux/platform_device.h
+#include linux/pwm.h
+#include linux/slab.h
+
+#define LP3943_MAX_DUTY255
+#define LP3943_MIN_PERIOD  6250
+#define LP3943_MAX_PERIOD  160
+
+struct lp3943_pwm {
+   struct pwm_chip chip;
+   struct lp3943 *lp3943;
+   struct lp3943_platform_data *pdata;
+};
+
+static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *_chip)
+{
+   return container_of(_chip, struct lp3943_pwm, chip);
+}
+
+static struct lp3943_pwm_map *
+lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
+{
+   struct lp3943_platform_data *pdata = lp3943_pwm-pdata;
+   struct lp3943 *lp3943 = lp3943_pwm-lp3943;
+   struct lp3943_pwm_map *pwm_map;
+   int i, offset;
+
+   pwm_map = kzalloc(sizeof(*pwm_map), GFP_KERNEL);
+   if (!pwm_map)
+   return ERR_PTR(-ENOMEM);
+
+   pwm_map-output = pdata-pwms[hwpwm]-output;
+   pwm_map-num_outputs = pdata-pwms[hwpwm]-num_outputs;
+
+   for (i = 0; i  pwm_map-num_outputs; i++) {
+   offset = pwm_map-output[i];
+
+   /* Return an error if the pin is already assigned */
+   if (test_and_set_bit(offset, lp3943-pin_used))
+   return ERR_PTR(-EBUSY);
+   }
+
+   return pwm_map;
+}
+
+static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+   struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
+   struct lp3943_pwm_map *pwm_map;
+
+   pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm-hwpwm);
+   if (IS_ERR(pwm_map))
+   return PTR_ERR(pwm_map);
+
+   return pwm_set_chip_data(pwm, pwm_map);
+}
+
+static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
+   struct lp3943_pwm_map *pwm_map)
+{
+   struct lp3943 *lp3943 = lp3943_pwm-lp3943;
+   int i, offset;
+
+   for (i = 0; i  pwm_map-num_outputs; i++) {
+