Return-path: Received: from mail.bootlin.com ([62.4.15.54]:53257 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727363AbeJRR3W (ORCPT ); Thu, 18 Oct 2018 13:29:22 -0400 Date: Thu, 18 Oct 2018 11:29:12 +0200 From: Maxime Ripard To: jacopo mondi Cc: Mauro Carvalho Chehab , Laurent Pinchart , linux-media@vger.kernel.org, Thomas Petazzoni , Mylene Josserand , Hans Verkuil , Sakari Ailus , Hugues Fruchet , Loic Poulain , Samuel Bobrowicz , Steve Longerbeam , Daniel Mack Subject: Re: [PATCH v4 01/12] media: ov5640: Adjust the clock based on the expected rate Message-ID: <20181018092912.u23arvx5ope24m5t@flea> References: <20181011092107.30715-1-maxime.ripard@bootlin.com> <20181011092107.30715-2-maxime.ripard@bootlin.com> <20181016165450.GB11703@w540> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="fkdb2cekh5vu3nxx" Content-Disposition: inline In-Reply-To: <20181016165450.GB11703@w540> Sender: linux-media-owner@vger.kernel.org List-ID: --fkdb2cekh5vu3nxx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Jacopo, Thanks for reviewing this patch On Tue, Oct 16, 2018 at 06:54:50PM +0200, jacopo mondi wrote: > > +static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor, > > + u8 pll_prediv, u8 pll_mult, > > + u8 sysdiv) > > +{ > > + unsigned long rate =3D clk_get_rate(sensor->xclk); >=20 > The clock rate is stored in sensor->xclk at probe time, no need to > query it every iteration. =46rom a clk API point of view though, there's nothing that guarantees that the clock rate hasn't changed between the probe and the time where this function is called. I appreciate that we're probably connected to an oscillator, but even then, on the Allwinner SoCs we've had the issue recently that one oscillator feeding the BT chip was actually had a muxer, with each option having a slightly different rate, which was bad enough for the BT chip to be non-functional. I can definitely imagine the same case happening here for some SoCs. Plus, the clock framework will cache the rate as well when possible, so we're not losing anything here. > > + > > + return rate / pll_prediv * pll_mult / sysdiv; > > +} > > + > > +static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor, > > + unsigned long rate, > > + u8 *pll_prediv, u8 *pll_mult, > > + u8 *sysdiv) > > +{ > > + unsigned long best =3D ~0; > > + u8 best_sysdiv =3D 1, best_mult =3D 1; > > + u8 _sysdiv, _pll_mult; > > + > > + for (_sysdiv =3D OV5640_SYSDIV_MIN; > > + _sysdiv <=3D OV5640_SYSDIV_MAX; > > + _sysdiv++) { > > + for (_pll_mult =3D OV5640_PLL_MULT_MIN; > > + _pll_mult <=3D OV5640_PLL_MULT_MAX; > > + _pll_mult++) { > > + unsigned long _rate; > > + > > + /* > > + * The PLL multiplier cannot be odd if above > > + * 127. > > + */ > > + if (_pll_mult > 127 && (_pll_mult % 2)) > > + continue; > > + > > + _rate =3D ov5640_compute_sys_clk(sensor, > > + OV5640_PLL_PREDIV, > > + _pll_mult, _sysdiv); >=20 > I'm under the impression a system clock slower than the requested one, ev= en > if more accurate is not good. >=20 > I'm still working on understanding how all CSI-2 related timing > parameters play together, but since the system clock is calculated > from the pixel clock (which comes from the frame dimensions, bpp, and > rate), and it is then used to calculate the MIPI BIT clock frequency, > I think it would be better to be a little faster than a bit slower, > otherwise the serial lane clock wouldn't be fast enough to output > frames generated by the sensor core (or maybe it would just decrease > the frame rate and that's it, but I don't think it is just this). >=20 > What do you think of adding the following here: >=20 > if (_rate < rate) > continue I really don't know MIPI-CSI2 enough to be able to comment on your concerns, but when reaching the end of the operating limit of the clock, it would prevent us from having any rate at all, which seems bad too. > > + if (abs(rate - _rate) < abs(rate - best)) { > > + best =3D _rate; > > + best_sysdiv =3D _sysdiv; > > + best_mult =3D _pll_mult; > > + } > > + > > + if (_rate =3D=3D rate) > > + goto out; > > + } > > + } > > + > > +out: > > + *sysdiv =3D best_sysdiv; > > + *pll_prediv =3D OV5640_PLL_PREDIV; > > + *pll_mult =3D best_mult; > > + return best; > > +} >=20 > These function gets called at s_stream time, and cycle for a while, > and I'm under the impression the MIPI state machine doesn't like > delays too much, as I see timeouts on the receiver side. >=20 > I have tried to move this function at set_fmt() time, every time a new > mode is selected, sysdiv, pll_prediv and pll_mult gets recalculated > (and stored in the ov5640_dev structure). I now have other timeouts on > missing EOF, but not anymore at startup time it seems. I have no objection caching the values if it solves issues with CSI :) Can you send that patch? Thanks! Maxime --=20 Maxime Ripard, Bootlin Embedded Linux and Kernel engineering https://bootlin.com --fkdb2cekh5vu3nxx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEE0VqZU19dR2zEVaqr0rTAlCFNr3QFAlvIUmcACgkQ0rTAlCFN r3TgaA//WbwerGmMRGQQCPCmHScxJnpa9V8sgOvZG0FnW2kYjNgvRQE7SZjQm5Aw 95xoeCAIInFWNoXxPc+WgQf4pQ2/TGZWnR5NnRgzOTxQOjcuQJcl2sP0xgwuQP+j 6ba0UIq4OJDCIlbbVDIG7M4Fc2gBqvyQykc4b4pAFxtXVvomSYXpBbT/hijHLXeJ iOgMaXPsF7tNQY3LpewNTgfq17NPdMxQI/OWBoxCsclJ6Es3nGqD15qMEm5d+GeB DRfCp98lfWi9QkhBAyJWZyGYXsZt8bJBmMQVxQJHAab5cOzeosIplAQ8k0U55rwN 2hHbOvvIrNRx+WJ5FXby6euO1rZPTCNP/GVRY+TtJPz8U/344Hp4c1vnL0aLb+xN TmtomPHmiZF6wqWwJij3IaCz++C4KJgYtQVM8pZa28z/mgxZ42t5Nawi5UjVdrNy Jun+HVzX8TrmibC9nbN4MvInZIPP7mUDVL+rPQtJotDkei5uWPCQnzKbr7sd3cdS m1+gx2NfLiRCm02vu+oLFUEFMYR9bHQo9oH3YVsQpUIYvXn9x7trZsjgwW5TDnrr AxK2Xcp2OMW8c7xYlxFq9OW142s46oXgGs6QjtejNdJH9I30YjuBojOA2yueclx3 l975LX9em8IMQLVdhDrSTtlcjjtjJnICpD2pdXqul511ryzIfkk= =7FeE -----END PGP SIGNATURE----- --fkdb2cekh5vu3nxx--