1/*
2 * isph3a.c
3 *
4 * TI OMAP3 ISP - H3A module
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc.
8 *
9 * Contacts: David Cohen <dacohen@gmail.com>
10 *	     Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 *	     Sakari Ailus <sakari.ailus@iki.fi>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/slab.h>
19#include <linux/uaccess.h>
20
21#include "isp.h"
22#include "isph3a.h"
23#include "ispstat.h"
24
25/*
26 * h3a_aewb_update_regs - Helper function to update h3a registers.
27 */
28static void h3a_aewb_setup_regs(struct ispstat *aewb, void *priv)
29{
30	struct omap3isp_h3a_aewb_config *conf = priv;
31	u32 pcr;
32	u32 win1;
33	u32 start;
34	u32 blk;
35	u32 subwin;
36
37	if (aewb->state == ISPSTAT_DISABLED)
38		return;
39
40	isp_reg_writel(aewb->isp, aewb->active_buf->dma_addr,
41		       OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWBUFST);
42
43	if (!aewb->update)
44		return;
45
46	/* Converting config metadata into reg values */
47	pcr = conf->saturation_limit << ISPH3A_PCR_AEW_AVE2LMT_SHIFT;
48	pcr |= !!conf->alaw_enable << ISPH3A_PCR_AEW_ALAW_EN_SHIFT;
49
50	win1 = ((conf->win_height >> 1) - 1) << ISPH3A_AEWWIN1_WINH_SHIFT;
51	win1 |= ((conf->win_width >> 1) - 1) << ISPH3A_AEWWIN1_WINW_SHIFT;
52	win1 |= (conf->ver_win_count - 1) << ISPH3A_AEWWIN1_WINVC_SHIFT;
53	win1 |= (conf->hor_win_count - 1) << ISPH3A_AEWWIN1_WINHC_SHIFT;
54
55	start = conf->hor_win_start << ISPH3A_AEWINSTART_WINSH_SHIFT;
56	start |= conf->ver_win_start << ISPH3A_AEWINSTART_WINSV_SHIFT;
57
58	blk = conf->blk_ver_win_start << ISPH3A_AEWINBLK_WINSV_SHIFT;
59	blk |= ((conf->blk_win_height >> 1) - 1) << ISPH3A_AEWINBLK_WINH_SHIFT;
60
61	subwin = ((conf->subsample_ver_inc >> 1) - 1) <<
62		 ISPH3A_AEWSUBWIN_AEWINCV_SHIFT;
63	subwin |= ((conf->subsample_hor_inc >> 1) - 1) <<
64		  ISPH3A_AEWSUBWIN_AEWINCH_SHIFT;
65
66	isp_reg_writel(aewb->isp, win1, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWWIN1);
67	isp_reg_writel(aewb->isp, start, OMAP3_ISP_IOMEM_H3A,
68		       ISPH3A_AEWINSTART);
69	isp_reg_writel(aewb->isp, blk, OMAP3_ISP_IOMEM_H3A, ISPH3A_AEWINBLK);
70	isp_reg_writel(aewb->isp, subwin, OMAP3_ISP_IOMEM_H3A,
71		       ISPH3A_AEWSUBWIN);
72	isp_reg_clr_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
73			ISPH3A_PCR_AEW_MASK, pcr);
74
75	aewb->update = 0;
76	aewb->config_counter += aewb->inc_config;
77	aewb->inc_config = 0;
78	aewb->buf_size = conf->buf_size;
79}
80
81static void h3a_aewb_enable(struct ispstat *aewb, int enable)
82{
83	if (enable) {
84		isp_reg_set(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
85			    ISPH3A_PCR_AEW_EN);
86		omap3isp_subclk_enable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
87	} else {
88		isp_reg_clr(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR,
89			    ISPH3A_PCR_AEW_EN);
90		omap3isp_subclk_disable(aewb->isp, OMAP3_ISP_SUBCLK_AEWB);
91	}
92}
93
94static int h3a_aewb_busy(struct ispstat *aewb)
95{
96	return isp_reg_readl(aewb->isp, OMAP3_ISP_IOMEM_H3A, ISPH3A_PCR)
97						& ISPH3A_PCR_BUSYAEAWB;
98}
99
100static u32 h3a_aewb_get_buf_size(struct omap3isp_h3a_aewb_config *conf)
101{
102	/* Number of configured windows + extra row for black data */
103	u32 win_count = (conf->ver_win_count + 1) * conf->hor_win_count;
104
105	/*
106	 * Unsaturated block counts for each 8 windows.
107	 * 1 extra for the last (win_count % 8) windows if win_count is not
108	 * divisible by 8.
109	 */
110	win_count += (win_count + 7) / 8;
111
112	return win_count * AEWB_PACKET_SIZE;
113}
114
115static int h3a_aewb_validate_params(struct ispstat *aewb, void *new_conf)
116{
117	struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
118	u32 buf_size;
119
120	if (unlikely(user_cfg->saturation_limit >
121		     OMAP3ISP_AEWB_MAX_SATURATION_LIM))
122		return -EINVAL;
123
124	if (unlikely(user_cfg->win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
125		     user_cfg->win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
126		     user_cfg->win_height & 0x01))
127		return -EINVAL;
128
129	if (unlikely(user_cfg->win_width < OMAP3ISP_AEWB_MIN_WIN_W ||
130		     user_cfg->win_width > OMAP3ISP_AEWB_MAX_WIN_W ||
131		     user_cfg->win_width & 0x01))
132		return -EINVAL;
133
134	if (unlikely(user_cfg->ver_win_count < OMAP3ISP_AEWB_MIN_WINVC ||
135		     user_cfg->ver_win_count > OMAP3ISP_AEWB_MAX_WINVC))
136		return -EINVAL;
137
138	if (unlikely(user_cfg->hor_win_count < OMAP3ISP_AEWB_MIN_WINHC ||
139		     user_cfg->hor_win_count > OMAP3ISP_AEWB_MAX_WINHC))
140		return -EINVAL;
141
142	if (unlikely(user_cfg->ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
143		return -EINVAL;
144
145	if (unlikely(user_cfg->hor_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
146		return -EINVAL;
147
148	if (unlikely(user_cfg->blk_ver_win_start > OMAP3ISP_AEWB_MAX_WINSTART))
149		return -EINVAL;
150
151	if (unlikely(user_cfg->blk_win_height < OMAP3ISP_AEWB_MIN_WIN_H ||
152		     user_cfg->blk_win_height > OMAP3ISP_AEWB_MAX_WIN_H ||
153		     user_cfg->blk_win_height & 0x01))
154		return -EINVAL;
155
156	if (unlikely(user_cfg->subsample_ver_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
157		     user_cfg->subsample_ver_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
158		     user_cfg->subsample_ver_inc & 0x01))
159		return -EINVAL;
160
161	if (unlikely(user_cfg->subsample_hor_inc < OMAP3ISP_AEWB_MIN_SUB_INC ||
162		     user_cfg->subsample_hor_inc > OMAP3ISP_AEWB_MAX_SUB_INC ||
163		     user_cfg->subsample_hor_inc & 0x01))
164		return -EINVAL;
165
166	buf_size = h3a_aewb_get_buf_size(user_cfg);
167	if (buf_size > user_cfg->buf_size)
168		user_cfg->buf_size = buf_size;
169	else if (user_cfg->buf_size > OMAP3ISP_AEWB_MAX_BUF_SIZE)
170		user_cfg->buf_size = OMAP3ISP_AEWB_MAX_BUF_SIZE;
171
172	return 0;
173}
174
175/*
176 * h3a_aewb_set_params - Helper function to check & store user given params.
177 * @new_conf: Pointer to AE and AWB parameters struct.
178 *
179 * As most of them are busy-lock registers, need to wait until AEW_BUSY = 0 to
180 * program them during ISR.
181 */
182static void h3a_aewb_set_params(struct ispstat *aewb, void *new_conf)
183{
184	struct omap3isp_h3a_aewb_config *user_cfg = new_conf;
185	struct omap3isp_h3a_aewb_config *cur_cfg = aewb->priv;
186	int update = 0;
187
188	if (cur_cfg->saturation_limit != user_cfg->saturation_limit) {
189		cur_cfg->saturation_limit = user_cfg->saturation_limit;
190		update = 1;
191	}
192	if (cur_cfg->alaw_enable != user_cfg->alaw_enable) {
193		cur_cfg->alaw_enable = user_cfg->alaw_enable;
194		update = 1;
195	}
196	if (cur_cfg->win_height != user_cfg->win_height) {
197		cur_cfg->win_height = user_cfg->win_height;
198		update = 1;
199	}
200	if (cur_cfg->win_width != user_cfg->win_width) {
201		cur_cfg->win_width = user_cfg->win_width;
202		update = 1;
203	}
204	if (cur_cfg->ver_win_count != user_cfg->ver_win_count) {
205		cur_cfg->ver_win_count = user_cfg->ver_win_count;
206		update = 1;
207	}
208	if (cur_cfg->hor_win_count != user_cfg->hor_win_count) {
209		cur_cfg->hor_win_count = user_cfg->hor_win_count;
210		update = 1;
211	}
212	if (cur_cfg->ver_win_start != user_cfg->ver_win_start) {
213		cur_cfg->ver_win_start = user_cfg->ver_win_start;
214		update = 1;
215	}
216	if (cur_cfg->hor_win_start != user_cfg->hor_win_start) {
217		cur_cfg->hor_win_start = user_cfg->hor_win_start;
218		update = 1;
219	}
220	if (cur_cfg->blk_ver_win_start != user_cfg->blk_ver_win_start) {
221		cur_cfg->blk_ver_win_start = user_cfg->blk_ver_win_start;
222		update = 1;
223	}
224	if (cur_cfg->blk_win_height != user_cfg->blk_win_height) {
225		cur_cfg->blk_win_height = user_cfg->blk_win_height;
226		update = 1;
227	}
228	if (cur_cfg->subsample_ver_inc != user_cfg->subsample_ver_inc) {
229		cur_cfg->subsample_ver_inc = user_cfg->subsample_ver_inc;
230		update = 1;
231	}
232	if (cur_cfg->subsample_hor_inc != user_cfg->subsample_hor_inc) {
233		cur_cfg->subsample_hor_inc = user_cfg->subsample_hor_inc;
234		update = 1;
235	}
236
237	if (update || !aewb->configured) {
238		aewb->inc_config++;
239		aewb->update = 1;
240		cur_cfg->buf_size = h3a_aewb_get_buf_size(cur_cfg);
241	}
242}
243
244static long h3a_aewb_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
245{
246	struct ispstat *stat = v4l2_get_subdevdata(sd);
247
248	switch (cmd) {
249	case VIDIOC_OMAP3ISP_AEWB_CFG:
250		return omap3isp_stat_config(stat, arg);
251	case VIDIOC_OMAP3ISP_STAT_REQ:
252		return omap3isp_stat_request_statistics(stat, arg);
253	case VIDIOC_OMAP3ISP_STAT_EN: {
254		unsigned long *en = arg;
255		return omap3isp_stat_enable(stat, !!*en);
256	}
257	}
258
259	return -ENOIOCTLCMD;
260}
261
262static const struct ispstat_ops h3a_aewb_ops = {
263	.validate_params	= h3a_aewb_validate_params,
264	.set_params		= h3a_aewb_set_params,
265	.setup_regs		= h3a_aewb_setup_regs,
266	.enable			= h3a_aewb_enable,
267	.busy			= h3a_aewb_busy,
268};
269
270static const struct v4l2_subdev_core_ops h3a_aewb_subdev_core_ops = {
271	.ioctl = h3a_aewb_ioctl,
272	.subscribe_event = omap3isp_stat_subscribe_event,
273	.unsubscribe_event = omap3isp_stat_unsubscribe_event,
274};
275
276static const struct v4l2_subdev_video_ops h3a_aewb_subdev_video_ops = {
277	.s_stream = omap3isp_stat_s_stream,
278};
279
280static const struct v4l2_subdev_ops h3a_aewb_subdev_ops = {
281	.core = &h3a_aewb_subdev_core_ops,
282	.video = &h3a_aewb_subdev_video_ops,
283};
284
285/*
286 * omap3isp_h3a_aewb_init - Module Initialisation.
287 */
288int omap3isp_h3a_aewb_init(struct isp_device *isp)
289{
290	struct ispstat *aewb = &isp->isp_aewb;
291	struct omap3isp_h3a_aewb_config *aewb_cfg;
292	struct omap3isp_h3a_aewb_config *aewb_recover_cfg;
293
294	aewb_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_cfg), GFP_KERNEL);
295	if (!aewb_cfg)
296		return -ENOMEM;
297
298	aewb->ops = &h3a_aewb_ops;
299	aewb->priv = aewb_cfg;
300	aewb->event_type = V4L2_EVENT_OMAP3ISP_AEWB;
301	aewb->isp = isp;
302
303	/* Set recover state configuration */
304	aewb_recover_cfg = devm_kzalloc(isp->dev, sizeof(*aewb_recover_cfg),
305					GFP_KERNEL);
306	if (!aewb_recover_cfg) {
307		dev_err(aewb->isp->dev, "AEWB: cannot allocate memory for "
308					"recover configuration.\n");
309		return -ENOMEM;
310	}
311
312	aewb_recover_cfg->saturation_limit = OMAP3ISP_AEWB_MAX_SATURATION_LIM;
313	aewb_recover_cfg->win_height = OMAP3ISP_AEWB_MIN_WIN_H;
314	aewb_recover_cfg->win_width = OMAP3ISP_AEWB_MIN_WIN_W;
315	aewb_recover_cfg->ver_win_count = OMAP3ISP_AEWB_MIN_WINVC;
316	aewb_recover_cfg->hor_win_count = OMAP3ISP_AEWB_MIN_WINHC;
317	aewb_recover_cfg->blk_ver_win_start = aewb_recover_cfg->ver_win_start +
318		aewb_recover_cfg->win_height * aewb_recover_cfg->ver_win_count;
319	aewb_recover_cfg->blk_win_height = OMAP3ISP_AEWB_MIN_WIN_H;
320	aewb_recover_cfg->subsample_ver_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
321	aewb_recover_cfg->subsample_hor_inc = OMAP3ISP_AEWB_MIN_SUB_INC;
322
323	if (h3a_aewb_validate_params(aewb, aewb_recover_cfg)) {
324		dev_err(aewb->isp->dev, "AEWB: recover configuration is "
325					"invalid.\n");
326		return -EINVAL;
327	}
328
329	aewb_recover_cfg->buf_size = h3a_aewb_get_buf_size(aewb_recover_cfg);
330	aewb->recover_priv = aewb_recover_cfg;
331
332	return omap3isp_stat_init(aewb, "AEWB", &h3a_aewb_subdev_ops);
333}
334
335/*
336 * omap3isp_h3a_aewb_cleanup - Module exit.
337 */
338void omap3isp_h3a_aewb_cleanup(struct isp_device *isp)
339{
340	omap3isp_stat_cleanup(&isp->isp_aewb);
341}
342